-
Notifications
You must be signed in to change notification settings - Fork 3
/
SDPdatabuildingtasks.Rmd
2860 lines (2141 loc) · 103 KB
/
SDPdatabuildingtasks.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "CLEAN: DATA BUILDING GUIDE"
author: "Strategic Data Project"
date: "Center for Education Policy Research at Harvard University"
output:
pdf_document:
toc: yes
toc_depth: 4
latex_engine: xelatex
includes:
in_header: harvardheader.tex
before_body: harvard_prefix.tex
html_document:
toc: yes
toc_depth: 4
---
# CLEAN: DATA BUILDING GUIDE
## Introduction
### SDP DATA BUILDING GUIDE
\normalsize
Congratulations on identifying the data elements that are essential for
conducting rigorous analyses in your organization. **Clean** is the next stage
in the SDP Toolkit for Effective Data Use. To successfully move through the
**Clean** stage, you should review the **Identify** component of this toolkit.
Upon completing this stage, you will have produced clean research files that
will allow you to **Connect** and **Analyze** data related to college-going
success in your agency.
#### The Tasks
**Clean** consist of five tasks that share a similar structure. The tasks are
geared toward analysts with at least moderately strong data background and
comfort with statistics. Each task provides hands-on experience building
specific components of the research file used for the SDP College Going
Diagnostic Analyses.
The tasks are listed as follows:
- Task 1: Student Attributes
- Task 2: Student School Year
- Task 3: Identifying the Ninth Grade Cohort
- Task 4: Student School Enrollment
- Task 5: Student Test Scores
- Task 6: Student Class Enrollment
- Task 7: NSC (National Student Clearinghouse) Data
Each task uses a raw input file and produces a cleaned
output file that matches Identify.
Download these raw input files along with everything else you need for the
toolkit as a zip file at
[sdp.cepr.harvard.edu/toolkit](http://www.sdp.cepr.harvard.edu/toolkit). When
unzipped, this file will reveal an infrastructure including all the steps of the
toolkit, the data files you need, and template files with R code.
In particular, in Clean, you will be working with the files in the **raw**
folder. If you are using R, you can fill in the corresponding `.R` scripts
in **programs** to go through the tasks.
#### How to Start
The beginning of the Data Building Guide is a Decision
Rules Glossary (p. 6). This glossary provides decision rules
for resolving data problems associated with particular
variables. It is meant to be a quick-reference guide of
rules that can be used with any software platform. These decision rules are then implemented in the step-by-step
instructions the tasks provide.
SDP has also created a detailed **SDP R Glossary**,
available as a separate document, that covers the R
commands used throughout the toolkit. Commands are
listed alphabetically and by subject.
As you go through a task, be sure to consult the data and code
snippets to get a visual sense for the changes occurring at each step.
#### Task Structure
The tasks follow a logical sequence from **1** to **7**. Each task
comes with its own raw input file that results in a cleaned
output file that matches or extends the file **Identify**. We
also provide all cleaned output files so you can check your
answers after completing each task. If you have followed the
task instructions correctly, you should arrive at the same
cleaned output file.
\vspace{8mm}
In each task, you will also find:
- **Purpose:** --- Clarifies the importance of the task.
- **How to Start:** --- Identifies the input file(s) for the task.
- **Data Description:** --- Describes data elements for the task.
- **Instructions:** --- Provides instructions to transform data.
These instructions include:
- R code to help you execute the instructions through code
- Data snapshots to help you visualize changes to the data at each step
#### Summary
Through the tasks, you will learn effective practices
for: data transformation, variable construction, and
implementation of key decision rules.
The **Task Map** on the next page summarizes the inputs
and outputs of each task and how the outputs are used in
**Connect** to produce an analysis file. The Task Map also
serves as a Table of Contents.
If you need additional guidance, the friendly research team at
SDP is available to help: **sdp@gse.harvard.edu** .
#### Task Map
This map summarizes the inputs and outputs for each task and how the outputs
are used in Connect to produce the college-going analysis and college-going
analysis on-track file.
![Taskmap](includes/img/DBTaskTaskMap.jpg)
## Decision Rules Glossary
\Large{Student\_Attributes}
\vspace{-8mm}
\scriptsize
\singlespace
\begin{longtable}{| p{.08\paperwidth} | p{.23\paperwidth} p{.23\paperwidth} |
p{.12\paperwidth} | }
\caption*{Demographic, cohort, and graduation data for students.} \\ \hline
\Centering \textbf{Data Element} & \Centering \textbf{Possible Scenario} &
\Centering \textbf{SDP Decision Rule} &
\Centering \textbf{Reference in Data Building Tasks} \\ \hline
\textbf{sid} & & & \\ \hline
\textbf{male} & Some students with conflicting genders may be observed. &
If more than one gender observed, report the modal gender. If multiple modes
report the most recent gender recorded. & \textbf{Task 1:} Student Attributes \\
\hline
\textbf{race\_ethnicity} & Some students with moer than one race\_ethnicity may be
observed. For the purposes of anlysis, SDP consider race\_ethnicity time
invariant. & If more than one category selected serially in time,
report the modal race/ethnicity. If multiple modes are observed,
report the most recent race/ethnicity recorded. & \textbf{Task 1:}
Student Attributes \\ \hline
\textbf{race} & Some students with more than one race may be ovserved. For
the purposes of analysis, SDP considers race time invariant. & If more than
one category selected serially in time, report the modal race. If multiple modes
are observed, report the most recent race recorded. & \\ \hline
\textbf{ethnicity} & Some students with more than one ethnicity may be ovserved.
For the purposes of analysis, SDP considers ethnicity time invariant. & If more
than one ethnicity observed, report the modal ethnicity. If multiple modes
are observed, the most recent ethnicity recorded. & \\ \hline
\textbf{birth\_date} & Some students with more than one birth date may be observed.
For the purposes of analysis, SDP considers birth\_date time invariant. &
If more than one birth date observed, report the modal birth date. If multiple
modes, report the most recent birth date recorded. When evaluating modal birth
date exclude birth dates that fall outside +/- four years of the expected birth
date given grade level and school year. & \\ \hline
\textbf{first\_9th\_school\_ year\_reported} & & & \\ \hline
\textbf{hs\_diploma} & & & \textbf{Task 1:} Student Attributes \\ \hline
\textbf{hs\_diploma\_date} & Some students with more than one diploma date may be
observed. For the purposes of analysis, SDP considers hs\_diploma\_date time
invariant. & If more than one diploma date is observed, report the first diploma
date. & \textbf{Task 1:} Student Attributes \\ \hline
\textbf{hs\_diploma\_type} & Some students with more than one diploma type may be
observed. For the purposes of the analysis, SDP considers hs\_diploma\_type
to be time invariant. & If more than one type is observed, report the type
associated with the first diploma date. If multiple diploma types are observed
for the first diploma date, report the modal value. If there is no mode, report the
most competitive diploma type. If there is a diploma date but no diploma type,
report the diploma type as "Unknown." & \textbf{Task 1:} Student Attributes \\ \hline
\textbf{zip\_code} & & & \\ \hline
\end{longtable}
\vspace{-6mm}
\small{Identifies unique observation: \textbf{sid}}
\normalsize
\vspace{5mm}
\Large{Student\_School\_Year}
\vspace{-8mm}
\scriptsize
\singlespace
\begin{longtable}{| p{.08\paperwidth} | p{.23\paperwidth} p{.23\paperwidth} |
p{.12\paperwidth} | }
\caption*{Yearly classification and attendance data for students.} \\ \hline
\Centering \textbf{Data Element} & \Centering \textbf{Possible Scenario} &
\Centering \textbf{SDP Decision Rule} &
\Centering \textbf{Reference in Data Building Tasks} \\ \hline
\textbf{sid} & & & \\ \hline
\textbf{school\_year} & & & \\ \hline
\textbf{grade\_level} & Some students with more than one grade level may be
observed during a given school year. & If more than one grade level observed
during a school year, report the highest grade level recorded. & \textbf{Task 2:}
Student School Year \\ \hline
\textbf{frpl} & Some students with more than one free or reduced price lunch
status may be observed during a given school year. & If more than one status is
observed during the school year, report the highest non-null value. &
\textbf{Task 2:} Student School Year \\ \hline
\textbf{iep} & Some studnets with more than one indivualized education plan status
may be observed during a given school year. & If both "no IEP" and "has IEP" are
observed during the same school year, report "had IEP". & \textbf{Task 2:}
Student School Year \\ \hline
\textbf{iep\_classification} & Some students with more than on individualized
education plan classification may be observed during a given school year. &
If more than one classification is observed during the school year, report the
last non-null classification reported. & \\ \hline
\textbf{ell} & Some students with more than one English language learner status
may be observed during a given school year. & If both "not ell" and "ell" are
observed during the school year, report "ell". & \textbf{Task 2:}
Student School Year \\ \hline
\textbf{ell\_classification} & Some students with more than one English language
learner classification may be observed during a given school year. & If more than
one classification is observed during the school year, report the last non-null
classification reported. & \\ \hline
\textbf{gifted} & Some students with more than one gifted status may be observed
during a given school year. & If both "not enrolled" and "enrolled" are observed
during the same school year, report "enrolled". & \textbf{Task 2:} Student
School Year \\ \hline
\textbf{gifted \_classification} & Some students with more than one gifted
classification may be observed during a given school year. & If more than
one classification is observed during the school year, report the last non-null
classification reported. & \\ \hline
\textbf{total\_days\_enrolled} & & If not reported, value may be calculated by
number of school days between enrollment\_date and withdrawal\_date, or
days\_present + days\_absent. & \textbf{Task 2:} Student School Year \\ \hline
\textbf{total\_days\_absent} & & Cannot exceed the number of days enrolled. &
\textbf{Task 2:} Student School Year \\ \hline
\textbf{days\_suspended\_ out\_of\_school} & & Cannot exceed the number of days
enrolled. & \textbf{Task 2:} Student School Year \\ \hline
\textbf{days\_suspended\_ in\_school} & & Cannot exceed the number of days
enrolled. & \textbf{Task 2:} Student School Year \\ \hline
\end{longtable}
\vspace{-6mm}
\small{Identifies unique observation: \textbf{sid + school\_year}}
\normalsize
\vspace{5mm}
\Large{Student\_School\_Enrollment}
\vspace{-8mm}
\scriptsize
\singlespace
\begin{longtable}{| p{.08\paperwidth} | p{.23\paperwidth} p{.23\paperwidth} |
p{.12\paperwidth} | }
\caption*{School enrollment/withdrawal data for students.} \\ \hline
\Centering \textbf{Data Element} & \Centering \textbf{Possible Scenario} &
\Centering \textbf{SDP Decision Rule} &
\Centering \textbf{Reference in Data Building Tasks} \\ \hline
\textbf{sid} & & & \\ \hline
\textbf{school\_year} & & & \\ \hline
\textbf{school\_code} & & & \\ \hline
\textbf{enrollment\_date} &
\multirow{6}{.23\paperwidth}{Many different scenarios may be
encountered:
\begin{itemize} \setlength\itemsep{-2pt}
\item Some enrollment spells may overlap at the same school.
\item Some enrollment spells may have a missing enrollment\_date and
withdrawal\_date.
\item Some enrollment spells may have the same enrollment\_date and withdrawal\_date.
\item Some enrollment spells may begin after the withdrawal\_date.
\item Some enrollment spells may begin after the end of the current school\_year
or before the beginning of the current school\_year.
\end{itemize}} &
\multirow{6}={If enrollment spells overlap at the same school, consolidate
enrollment observations into one enrollment period, using the earliest enrollment
date and last withdrwal date and their corresponding codes and descriptions. \\
\vspace{50mm}
Drop any other abnormal enrollment observations.} &
\multirow{6}={\textbf{Task 4:} Student School Enrollment} \\
\textbf{enrollment\_code} & & & \\[3ex]
\textbf{enrollment\_code \_desc} & & & \\[3ex]
\textbf{withdrawal\_date} & & & \\[3ex]
\textbf{withdrawal\_code} & & & \\[3ex]
\textbf{withdrawal\_code \_desc} & & & \\[3ex] \hline
\textbf{days\_enrolled} & & Update days enrolled after you have consolidated
overlapping enrollment observations. & \textbf{Task 4:} Student School Enrollment
\\ \hline
\textbf{days\_present} & & & \\ \hline
\end{longtable}
\vspace{-6mm}
\small{Identifies unique observation: \textbf{sid + school\_year + school\_code +
enrollment\_date}}
\normalsize
\vspace{5mm}
\Large{Student\_Test\_Scores}
\scriptsize
\singlespace
\begin{longtable}{| p{.08\paperwidth} | p{.23\paperwidth} p{.23\paperwidth} |
p{.12\paperwidth} | }
\caption*{Standardized test data for students (state standardized tests,
advanced placement, SAT, ACT, etc). Every attempt at a
test by a student should be recorded.} \\ \hline
\Centering \textbf{Data Element} & \Centering \textbf{Possible Scenario} &
\Centering \textbf{SDP Decision Rule} &
\Centering \textbf{Reference in Data Building Tasks} \\ \hline
\textbf{sid} & & & \\ \hline
\textbf{test\_code} & & & \\ \hline
\textbf{test\_date} & Students who re-take tests in the same year or are retained
in a grade and re-take the same test in different years may have multiple
observations for a single test code. & Take the earliest test in both cases. &
\textbf{Task 5:} Student Test Scores \\ \hline
\textbf{test\_code\_desc} & & & \\ \hline
\textbf{test\_type} & & & \textbf{Task 5:} Student Test Scores \\ \hline
\textbf{grade\_level} & & & \textbf{Task 5:} Student Test Scores \\ \hline
\textbf{test\_subject} & & & \textbf{Task 5:} Student Test Scores \\ \hline
\textbf{test\_version} & & & \textbf{Task 5:} Student Test Scores \\ \hline
\textbf{language\_version} & & & \textbf{Task 5:} Student Test Scores \\ \hline
\textbf{raw\_score} & \multirow{2}={
The raw score or scaled score may be missing or take on values outside the
accepted range. Additionally, a test observation may have a missing raw test score
but not a missing scaled and vice versa.} &
\multirow{2}={
Set scores outside the accepted range to missing. For instance, in the synthetic
data, a test score of 0 is considered outside the accepted range and is set to
missing. Based on knowledge of your agency, decide if all test observations should
have both raw and scaled scores. One option would be to drop test observations
missing either of the test scores. Here we only drop test observations that are
missing both test scores} & \textbf{Task 5:} Student Test Scores \\[10ex]
\textbf{scaled\_score} & & & \textbf{Task 5:} Student Test Scores \\[10ex] \hline
\textbf{performance\_level} & & & \textbf{Task 5:} Student Test Scores \\ \hline
\textbf{standardized\_score} & The system may not provide a standardized score
(i.e. mean zero, s.d. one). & Generate a standardized test score by subject,
test type, and school year. & \\ \hline
\end{longtable}
\vspace{-6mm}
\small{Identifies unique observation: \textbf{sid + test\_code + test\_date}}
\normalsize
\vspace{40mm}
\Large{Class}
\vspace{-8mm}
\scriptsize
\singlespace
\begin{longtable}{| p{.08\paperwidth} | p{.23\paperwidth} p{.23\paperwidth} |
p{.12\paperwidth} | }
\caption*{Class level scheduling data.} \\ \hline
\Centering \textbf{Data Element} & \Centering \textbf{Possible Scenario} &
\Centering \textbf{SDP Decision Rule} &
\Centering \textbf{Reference in Data Building Tasks} \\ \hline
\textbf{cid} & & & \textbf{Task 6:} Student Class Enrollment \\ \hline
\textbf{school\_year} & & & \textbf{Task 6:} Student Class Enrollment \\ \hline
\textbf{school\_code} & & & \textbf{Task 6:} Student Class Enrollment \\ \hline
\textbf{course\_code} & & & \textbf{Task 6:} Student Class Enrollment \\ \hline
\textbf{course\_code \_desc} & Agencies often have a very large number of
course names, in some cases, other criteria (e.g. department the course is
listed in or length of the course) is needed to identify a course. &
Use agency rules to flag math and English courses. &
\textbf{Task 6:} Student Class Enrollment \\ \hline
\textbf{section\_code} & & & \\ \hline
\textbf{period\_bell} & & & \\ \hline
\textbf{room\_number} & & & \\ \hline
\textbf{tid} & & & \\ \hline
\textbf{semester\_term \_year} & & &
\textbf{Task 6:} Student Class Enrollment \\ \hline
\textbf{graduation \_requirement} & & & \\ \hline
\textbf{credits\_possible} & & &
\textbf{Task 6:} Student Class Enrollment \\ \hline
\textbf{instructional\_level} & & & \\ \hline
\textbf{subject} & & &
\textbf{Task 6:} Student Class Enrollment \\ \hline
\end{longtable}
\vspace{-6mm}
\small{Identifies unique observation: \textbf{cid}}
\normalsize
\vspace{15mm}
\Large{Student\_Class\_Enrollment}
\vspace{-8mm}
\scriptsize
\singlespace
\begin{longtable}{| p{.08\paperwidth} | p{.23\paperwidth} p{.23\paperwidth} |
p{.12\paperwidth} | }
\caption*{Class enrollment, grades, and credits earned data for students.} \\ \hline
\Centering \textbf{Data Element} & \Centering \textbf{Possible Scenario} &
\Centering \textbf{SDP Decision Rule} &
\Centering \textbf{Reference in Data Building Tasks} \\ \hline
\textbf{sid} & & & \textbf{Task 6:} Student Class Enrollment \\ \hline
\textbf{cid} & & & \textbf{Task 6:} Student Class Enrollment \\ \hline
\textbf{class\_enrollment \_date} & Some class enrollment spells may overlap,
students may have more than one class\_enrollment\_date and/or more than one
class\_withdrawal\_date for the same cid, school\_year, and marking\_period. &
For overlapping class enrollment spells, report the earliest enrollment date
and latest withdrawal date. & \textbf{Task 6:} Student Class Enrollment \\ \hline
\textbf{class\_withdrawal \_date} & Some class enrollment spells may overlap,
students may have more than one class\_enrollment\_date and/or more than one
class\_withdrawal\_date for the same cid, school\_year, and marking\_period. &
For overlapping class enrollment spells, report the earliest enrollment date
and latest withdrawal date. & \textbf{Task 6:} Student Class Enrollment \\ \hline
\textbf{credits\_earned} & & & \textbf{Task 6:} Student Class Enrollment \\ \hline
\textbf{final\_grade\_mark} & & & \textbf{Task 6:} Student Class Enrollment \\ \hline
\end{longtable}
\vspace{-6mm}
\small{Identifies unique observation: \textbf{sid + cid + enrollment\_date}}
\normalsize
\vspace{80mm}
\Large{Student\_NSC\_Enrollment}
\scriptsize
\singlespace
\begin{longtable}{| p{.08\paperwidth} | p{.23\paperwidth} p{.23\paperwidth} |
p{.12\paperwidth} | }
\caption*{National Student Clearing House Student Tracker
student-level data that provides information on postsecondary outcomes.} \\ \hline
\Centering \textbf{Data Element} & \Centering \textbf{Possible Scenario} &
\Centering \textbf{SDP Decision Rule} &
\Centering \textbf{Reference in Data Building Tasks} \\ \hline
\textbf{sid} & & & \textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{college\_code\_branch} & & & \textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{enrollment\_begin} & & Identify the students's first college by the earliest
enrollment date. & \textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{enrollment\_end} & & & \textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{record\_found\_yn} & & & \textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{high\_school\_code} & & & \\ \hline
\textbf{high\_school \_grad\_dt} & & & \\ \hline
\textbf{college\_name} & & & \textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{college\_state} & & Create an indicator for in-state and one for out of
state colleges. & \textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{year4year} & & Create two indicators to mark 4-year colleges and 2-year
colleges. Combine "2-year" and "Less than 2 years". & \textbf{Task 7:} Student
NSC Enrollment \\ \hline
\textbf{public\_private} & & Create two indicators for the two types. &
\textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{enrollment\_status} & & Sometimesstudents enroll in more than one college
at the same time. When identifying first college, report the one with the highest
enrollment status (F, H, L, in order of importance) and then by longest duration.
& \textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{graduated} & & & \textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{graduation\_date} & & & \textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{college\_sequence} & & & \textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{degree\_title} & & & \textbf{Task 7:} Student NSC Enrollment \\ \hline
\textbf{major} & & & \textbf{Task 7:} Student NSC Enrollment \\ \hline
\end{longtable}
\vspace{-6mm}
\small{Identifies unique observation: \textbf{sid + college\_code\_branch +
enrollment\_begin + enrollment\_end}}
\normalsize
## Task 1: STUDENT ATTRIBUTES
### PURPOSE
In **Task 1: Student Attributes**, you will take the `Student_Demographics_Raw`
file and generate the clean `Student_Attributes` file that matches the
specification in **Identify** with one observation per student.
The core of this task:
1. Create consistent gender indicators for students across years.
2. Create consistent race/ethnicity values for students across years.
3. Create consistent values for high school diploma indicators.
### HOW TO START
To begin, open the `Student_Demographics_Raw` file in R. If you do not have R,
you can follow the steps of the task by looking at the instructions and data
snippets we have provided.
If this is your first time attempting **Task 1**, start with the provided raw
input file. This file teaches you SDP’s cleaning methodology and allows you to
check answers from a common dataset.
```{r environmentSetup, echo=FALSE, error=FALSE, message=FALSE, warning=FALSE, comment=NA}
# Set options for R code output
library(knitr)
knitr::opts_chunk$set(comment=NA, warning=FALSE,
error=FALSE, message=FALSE, echo=TRUE,
fig.align='center')
# Set R output width to render nicely
options(width=80)
```
### DATA DESCRIPTION
The clean `Student_Attributes` file includes `sid`, `male`, `race_ethnicity`,
`first_9th_school_year_reported`, `hs_diploma`, `hs_diploma_date`, and
`hs_diploma_type`. Later analyses do not currently make use of birth dates and
zip codes, and these variables are thus excluded. This file contains the
combined `race_ethnicity` variable rather than separate variables for race
and ethnicity.
The raw input file, `Student_Demographics_Raw`, varies from the clean
`Student_Attributes` file in a number of ways. In `Student_Demographics_Raw`,
`race_ethnicity` is coded as a string rather than numeric and does not
distinguish between the designations multiple,
"M", and other, "O". `Student_Demographics_Raw` is also a time-variant data
set including `school_year` so the data is unique by `sid` and `school_year`.
`Student_Attributes`, however, is unique by `sid` alone. The aim of this
task will be to match `Student_Attributes` to be unique by `sid` only.
#### Uniqueness
Some agencies may record **race_ethnicity** and/or **gender** each school year.
Alternatively, students may have multiple records for having attended ninth
grade or multiple diploma dates and/or types. To fix this issue, you will create
a `Student_Attributes` research file unique by `sid` alone starting from a
`Student_Demographics_Raw` file that is unique by `sid` and `school_year`. Once
the file is unique by `sid` as shown in **Identify**, it is ready for
**Connect**.
#### Step 0: Load and Inspect the Data
```{r loadRequiredPackages}
# Step 0: Load the packages and prepare your R environment
library(tidyverse) # main suite of R packages to ease data analysis
library(magrittr) # allows for some easier pipelines of data
# Read in some R functions that are useful for toolkit tasks, see SDP R Glossary
# for details
source("R/functions.R")
library(haven) # required for importing .dta files
```
```{r readStudentDemogFile, echo=TRUE}
# Step 0: Load the college-going analysis file into Stata
# using the haven library
# To read data from a zip file and unzip it in R we can
# create a connection to the path of the zip file
# To read data from a zip file we create a connection to the path of the
# zip file
tmpfileName <- "raw/Student_Demographics_Raw.dta"
# This assumes analysis is a raw subfolder from where the file is read,
# in this case inside the zipfile
con <- unz(description = "data/raw.zip", filename = tmpfileName,
open = "rb")
# The zipfile is located in the subdirectory data, called raw.zip
stuatt <- read_stata(con) # read data in the data subdirectory
close(con) # close the connection to the zip file, keeps data in memory
```
```{r inspectTheData}
glimpse(stuatt)
head(stuatt)
```
```{r checkUniqueness}
# Checks that number of unique values of `sid` equals number of rows
# A quick way to test this in R
n_distinct(stuatt$sid) == nrow(stuatt) #n_distinct function is in dplyr package
```
Now drop the `first_9th_school_year_reported` variable. You will create a
`first_9th_school_year_reported` variable in Task 3 that also imputes this
variable for transfer-ins.
```{r dropFirst9th}
# In R one way to drop a variable is by assigning it a NULL value
stuatt$first_9th_school_year_reported <- NULL
# For testing purposes, let's specify a variable which indexes the SIDs
# we will use to check our work
idx <- c(2, 8552, 12506) # Specify which SIDs are interesting
# Now we can easily view only relevant data
stuatt[stuatt$sid %in% idx,]
```
#### Step 1: Create one consistent value for gender across years
```{r inspectMaleVariable}
# Create one consistent value for gender for each student across years
# View the data
stuatt %>% arrange(sid, school_year) %>%
select(sid, school_year, male) %>%
filter(sid %in% idx)
```
Create a variable that shows how many unique values `male` assumes for each
student. Name this variable `nvals_male`. Tabulate the variable and browse
the relevant data.
```{r countUniqMale}
# Step 1: Create an intermediate variable that counts the number of unique
# values observed for `male` per student
stuatt <- stuatt %>% group_by(sid) %>%
mutate(nvals_male = length(unique(male))) %>% ungroup()
table(stuatt$nvals_male)
# Look at the values where more than one value is observed
stuatt %>% select(sid, school_year, male, nvals_male) %>%
filter(nvals_male > 1)
# Or interactively in RStudio
# stuatt %>% select(sid, school_year, male, nvals_male) %>%
# filter(nvals_male > 1) %>% View
```
Identify the modal gender. If multiple modes exist for a student, report the
most recent gender recorded.
```{r modalGender}
# Step 2: Identify the modal gender, if multiple modes exist, report the most
# recent gender
# Here is an example mode function in R taht mimics Stata
# We can read this function in or load it from another package
# library(eeptools)
# statamode creates a list of the modal values and assigns NA, missing,
# if more than one mode exists
statamode <- function(x) {
z <- table(as.vector(x))
m <- names(z)[z == max(z)]
if(length(m) == 1){
if(class(x) %in% c("numeric", "integer", "logical")){
class(m) <- class(x)
} else {
class(m) <- "character"
}
return(m)
}
return(NA)
}
# Apply statamode to the data grouped by sid
stuatt <- stuatt %>% group_by(sid) %>%
mutate(nvals_male = length(unique(male)),
male_mode = statamode(male)) %>% ungroup()
# Check our work
stuatt %>% select(sid, male, male_mode, nvals_male) %>%
filter(sid %in% idx)
# Replace male with male_mode where male_mode is not missing
# In R we replace by vector so both sides of the <- have to have the same filter
# so they are the same length, otherwise R will recycle the elements on the
# right hand side and we will have the wrong values in place
stuatt$male[!is.na(stuatt$male_mode)] <-
stuatt$male_mode[!is.na(stuatt$male_mode)]
```
```{r multipleModes}
idx <- c(8552, 12506)
stuatt %>% select(sid, school_year, male, nvals_male, male_mode) %>%
filter(sid %in% idx)
# If multiple modes exist, report the most recent gender recorded
stuatt %<>% arrange(sid, school_year) %>%
group_by(sid) %>%
mutate(temp_male_last = male[school_year == max(school_year)])
# Show sid 12506
stuatt %>% select(sid, school_year, male, nvals_male, male_mode, temp_male_last) %>%
filter(sid == 12506)
# Assign temp_male_last to the male variable in cases where no mode exists
stuatt$male[is.na(stuatt$male_mode)] <- stuatt$temp_male_last[is.na(stuatt$male_mode)]
# Check our work again
stuatt %>% select(sid, school_year, male, nvals_male, male_mode, temp_male_last) %>%
filter(sid == 12506)
# Drop temporary variables
stuatt %<>% select(-nvals_male, -male_mode, -temp_male_last)
```
Now check our work
```{r verifyGender}
table(stuatt$male)
# Check nvals without creating the variable
stuatt %>% ungroup %>%
group_by(sid) %>%
summarize(nvals = n_distinct(male)) %>% select(nvals) %>%
table
n_distinct(stuatt$sid)
```
##### Step 2: Create one consistent value for race_ethnicicty
Recode the raw `race_ethnicity` variable as a numeric variable and label it.
Replace the string race_ethnicity variable with the numeric one.
- 1 = African American, not Hispanic
- 2 = Asian American
- 3 = Hispanic
- 4 = American Indian
- 5 = White, not Hispanic
- 6 = Multiple / Other
```{r raceEthnicityRecode}
# When R reads in Stata files using haven it creates a data type called
# labelled, for compatibility with Stata and most R functions, we convert
# this into a more standard factor variable
# Create a copy
stuatt$race_num <- stuatt$race_ethnicity
stuatt$race_ethnicity <- as_factor(stuatt$race_ethnicity)
table(stuatt$race_ethnicity) #check current values
stuatt$race_num <- NA
stuatt$race_num[stuatt$race_ethnicity=='B'] <- 1
stuatt$race_num[stuatt$race_ethnicity=='A'] <- 2
stuatt$race_num[stuatt$race_ethnicity=='H'] <- 3
stuatt$race_num[stuatt$race_ethnicity=='NA'] <- 4
stuatt$race_num[stuatt$race_ethnicity=='W'] <- 5
stuatt$race_num[stuatt$race_ethnicity=='M/O'] <- 6
table(stuatt$race_num)
idx <- c(8552)
stuatt %>% filter(sid %in% idx) %>%
select(sid, school_year, race_ethnicity, race_num)
```
```{r raceFactorconstruction}
# If the data were not coming from Stata, we would need to create a factor
# variable ourselves
# In R categorical variables are best represented as factors
# Factors can have values, order, and labels
# Create a labeled factor for the new race_num variable
stuatt$race_num2 <- factor(stuatt$race_num,
labels = c('Black', 'Asian', 'Hispanic',
'Native American', 'White', 'MultipleOther'))
# Compare them to check using a cross-tabulation
table(stuatt$race_ethnicity, stuatt$race_num2)
# Replace them
stuatt$race_ethnicity <- stuatt$race_num2
stuatt$race_num2 <- NULL
table(stuatt$race_ethnicity) # counts
prop.table(table(stuatt$race_ethnicity))*100 #percentages
```
Check: What does the distribution of your `race_ethnicity` variable look like?
Let's redraw the tables above in a more readable format.
```{r prettyTable, results="markup"}
library(pander) # library to beautify output
pander(prop.table(table(stuatt$race_ethnicity))*100, style = "rmarkdown")
pander(table(stuatt$race_ethnicity), style = "rmarkdown")
```
Let's also draw a figure to show this distribution.
```{r prettyGraph, fig.align='center', fig.width=7, fig.height=5}
library(ggplot2) # the best R library for plotting
qplot(stuatt$race_ethnicity,geom='bar') +
theme_classic() + labs(x = 'Race/Ethnicity', y = 'Count',
title = "Frequency of Student Race")
```
Create a variable indicating how many unique values `race_ethnicity`
assumes for each student called `nvals_race`.
```{r nvalsRace}
# Create a variable indicating how many unique values `race_ethnicity` takes
# for each student
stuatt <- stuatt %>% group_by(sid) %>%
mutate(nvals_race = n_distinct(race_ethnicity))
table(stuatt$nvals_race)
```
Create a variable that shows how many unique values `race_ethnicity`
assumes for each student and `school_year`. Name this variable `nvals_race_yr`.
Tabulate the variable and browse the relevant data.
```{r nvalsRaceYear}
# Create a variable that shows how many unique values `race_ethnicity`
# assumes for each student and school year.
stuatt <- stuatt %>% group_by(sid, school_year) %>%
mutate(nvals_race_yr = n_distinct(race_ethnicity))
#Make a table
table(stuatt$nvals_race_yr)
# Browse the results
stuatt %>% select(sid, school_year, race_ethnicity, nvals_race, nvals_race_yr) %>%
filter(nvals_race_yr > 1)
```
If more than one race is reported in the same `school_year`, report students as
multiracial, unless one of their reported `race_ethnicity` values is Hispanic.
Report the student as Hispanic in that case.
```{r multiracialLabel}
# Generate a temporary hispanic variable
# Use ifelse function to recode variable
stuatt$temp_ishispanic <- ifelse(stuatt$race_num == 3 &
stuatt$nvals_race_yr > 1, 1, 0)
stuatt %>% select(sid, school_year, race_ethnicity, nvals_race,
nvals_race_yr, temp_ishispanic) %>%
filter(nvals_race_yr > 1)
# Take the maximum value of temp_ishispanic by student by school_year
# This is creating a variable indicating if the student was ever
# listed as hispanic in a given school year
stuatt %<>% group_by(sid, school_year) %>%
mutate(ishispanic = max(temp_ishispanic, na.rm=TRUE))
stuatt %>% select(sid, school_year, race_ethnicity, nvals_race, nvals_race_yr,
temp_ishispanic, ishispanic) %>%
filter(nvals_race_yr > 1)
# Replace hispanic values
stuatt$race_num[stuatt$nvals_race_yr > 1 & stuatt$ishispanic == 1] <- 3
stuatt$race_ethnicity[stuatt$nvals_race_yr > 1 & stuatt$ishispanic == 1] <- "Hispanic"
stuatt$race_num[stuatt$nvals_race_yr > 1 & stuatt$ishispanic != 1] <- 6
stuatt$race_ethnicity[stuatt$nvals_race_yr > 1 & stuatt$ishispanic != 1] <- "MultipleOther"
# Drop the temporary variables
stuatt <- select(stuatt, -ishispanic, -temp_ishispanic)
# Drop the duplicates resulting from fixing student with different race_ethnicity
# within a school year
# bind_rows allows us to bind two data frames with the same columns together
# The first data.frame will be all rows where the student-school_year race
# is consistent
# The second data.frame is all rows where student race varies by school_year,
# but we have corrected it and drop all duplicated rows using the distinct
# command
#NROW 87534
stuatt <- bind_rows(stuatt %>% filter(nvals_race_yr < 2),
stuatt %>% filter(nvals_race_yr > 1) %>%
distinct(sid, school_year, race_ethnicity, .keep_all=TRUE))
stuatt <- select(stuatt, -nvals_race_yr)
# Re arrange after binding the rows
stuatt %<>% arrange(sid, school_year)
```
```{r checkwork}
# Before we fixed the data we had 87534 rows
# We had 3 students with 2 different races, so we had 6 rows where we needed 3
# This means we had 3 extra rows
nrow(stuatt) == 87534 - 3
```
Report the modal race. If multiple modes exist for a student, report the most
recent race recorded.
```{r modalRace}
# Calculate the modal race for a student over time, if multiple modes exist
# report the most recent
stuatt %<>% group_by(sid) %>%
mutate(race_mode = statamode(race_ethnicity))
# tab1 <- table(modes$race_temp,modes$nvals)
# addmargins(tab1, FUN=list(Total=sum), quiet=TRUE)
stuatt %>% filter(sid == 8552) %>%
select(sid, school_year, race_ethnicity, nvals_race, race_mode)
stuatt$race_ethnicity[!is.na(stuatt$race_mode)] <- stuatt$race_mode[!is.na(stuatt$race_mode)]
stuatt %>% filter(sid == 8552) %>%
select(sid, school_year, race_ethnicity, nvals_race, race_mode)
# Consider cases where the mode is not unique
stuatt %>% filter(sid == 2) %>%
select(sid, school_year, race_ethnicity, nvals_race, race_mode)
```
Find the most recent race.
```{r mostRecentRace}
# Define the most recent value of race observed
stuatt %<>% group_by(sid) %>%
mutate(race_last = race_ethnicity[school_year == max(school_year)])
stuatt %>% filter(sid == 2) %>%
select(sid, school_year, race_ethnicity, nvals_race, race_mode, race_last)
stuatt$race_ethnicity[is.na(stuatt$race_mode)] <- stuatt$race_last[is.na(stuatt$race_mode)]
stuatt %>% filter(sid %in% c(8552, 2)) %>%
select(sid, school_year, race_ethnicity)
# Drop temporary variables
stuatt %<>% select(-nvals_race, -race_mode, -race_last, -race_num)
```
Check your work.
```{r raceTableFinal}
table(stuatt$race_ethnicity)
```
#### Step 3: Create consistent values for high school diploma
Recode the `hs_diploma_type variable` as a numeric variable and label it.
Replace the string `hs_diploma_type` variable with the numeric one. Use lower
numbers for more competitive diploma types.
```{r convertDipltoFactor}
# 1. Recode the `hs_diploma_type variable` as a numeric variable and label it.
# Replace the string `hs_diploma_type` variable with the numeric one. Use lower
# numbers for more competitive diploma types.
# In R a factor variable behaves like a labeled numeric variable in Stata
# When reading the data in from a .dta file we can recover the numeric
# labels and ordering by using the `as_factor` function
stuatt$dipl_num <- as_factor(stuatt$hs_diploma_type)
```
```{r longRecodeDiploma}
# To show the work this saves if the data has already been labeled in Stata,
# the alternative method for manually recreating this is shown below
stuatt$dipl_num <- 4
stuatt$dipl_num <- ifelse(stuatt$hs_diploma_type == "College Prep Diploma",
1, stuatt$dipl_num)
stuatt$dipl_num <- ifelse(stuatt$hs_diploma_type == "Standard Diploma",
2, stuatt$dipl_num)
stuatt$dipl_num <- ifelse(stuatt$hs_diploma_type == "Alternative Diploma",
3, stuatt$dipl_num)
stuatt %>% select(sid, school_year, hs_diploma, hs_diploma_date,
hs_diploma_type, dipl_num) %>%
filter(sid == 16)
stuatt$hs_diploma_type <- NULL
stuatt$hs_diploma_type <- stuatt$dipl_num
stuatt$dipl_num <- NULL
stuatt %>% select(sid, school_year, hs_diploma, hs_diploma_date,
hs_diploma_type) %>%
filter(sid == 16)
```