Some parts of code make take a very long time (in my case, 15 hours), so don’t be surprised and don’t tell me I haven’t warned you :)
** change the following to the directory where the .do files are
** located:
global dira "|your directory|"
** change the following to the directory where the .data files are
** located:
global dirb "|your directory|"
** change the following to the directory where you want to save the
** output files:
global dirc "|your directory|"
cd "$dirb"
use "atusact0318.dta", clear
destring tucaseid, gen(id) float
drop id
gen ID = tucaseid
sort ID tuactivity_n
gen start= .
replace start = 0 if tuactivity_n==1
gen stop = .
replace stop = tuactdur24 if tuactivity_n==1
sum tuactivity_n
forvalues i = 1/`r(max)' {
by ID: replace start = stop[_n-1] if start==.
by ID: replace stop = start+tuactdur24 if stop==.
}
gen Activity = .
*1 - sleep
replace Activity = 1 if trtier2p==101
*2 - personal care
replace Activity =2 if trtier2p==102|trtier2p==103|trtier2p==105|trtier2p==199
*3 - sex
replace Activity =3 if trtier2p==104
*4 - housework
replace Activity = 4 if trtier1p==2
*5 - child care
replace Activity = 5 if trtier2p==301 | trtier2p==302 | trtier2p==303 |trtier2p==401|trtier2p==402|trtier2p==403
*6 - elder care
replace Activity = 6 if trtier2p==304 | trtier2p==305 | trtier2p==399 |trtier2p==404|trtier2p==405|trtier2p==499
*7 - work
replace Activity = 7 if trtier1p==5
*8 - education
replace Activity = 8 if trtier1p==6
*9 - shopping
replace Activity = 9 if trtier1p==7
*10 - services
replace Activity = 10 if trtier1p==8|trtier1p==9|trtier1p==10
*11 - eating
replace Activity = 11 if trtier1p==11
*12 - leisure
replace Activity = 12 if (trtier1p==12|trtier1p==13|trtier1p==14|trtier1p==15|trtier1p==16) & (trcodep!=120303 |trcodep!=120304)
*13 - travel
replace Activity = 13 if trtier1p==18
*14 - other
replace Activity = 14 if trtier1p==50
*15 - TV
replace Activity =15 if trcodep==120303 |trcodep==120304
keep ID tuactivity_n Activity start stop tuactdur24
/**the next line creates the number of observations 1440 for each ID*/
* the number should be 289,657,440,
expand tuactdur24
sort ID tuactivity_n start
by ID: gen time = _n
keep ID time Activity
reshape wide Activity, i(ID) j(time)
/*the reshaping will take forever, on my laptop - 15 hours*/
save "ATUS 0318 sequences.dta"
**********************************************************************************************
******** use only this part to make tempograms
**********************************************************************************************
cd "$dirb"
use "ATUS 0318 sequences.dta", clear
lab def Activity 1 "Sleep" ///
2 "Personal Care" ///
3 "Sex" ///
4 "Housework" ///
5 "Child Care" ///
6 "Elder Care" ///
7 "Work" ///
8 "Education" ///
9 "Shopping" ///
10 "Services" ///
11 "Eating" ///
12 "Leisure" ///
13 "Travel" ///
14 "Other" ///
15 "TV"
forval i = 1/1440{
lab val Activity`i' Activity
}
save "ATUS 0318 sequences.dta", replace
clear
cd "$dirc"
save temp, replace emptyok
/*the command below will also take a pretty decent amount of time*/
forvalues i = 1(1)1440 {
cd "$dirb"
use "ATUS 0318 sequences.dta", clear
cd "$dirc"
quietly tab Activity`i', matcell(a)
svmat double a
keep a1
keep in 1/15
rename a1 a
append using temp
save temp, replace
}
use temp, clear
recode a (.=0)
************************************************************************
*** working with the resulting table
************************************************************************
ssc install seq
seq b, f(1) t(15)
seq x1, f(1440) t(1) b(15)
order b x1 a
reshape wide a, i(x1) j(b)
**** dealing with labels *** creating some of them
forval i = 0(1)23 {
local a = `i'*60+1
label def tod `a' "`i':00", modify
}
forval i = 0(1)23 {
local a = `i'*60+11
label def tod `a' "`i':10", modify
}
forval i = 0(1)23 {
local a = `i'*60+21
label def tod `a' "`i':20", modify
}
forval i = 0(1)23 {
local a = `i'*60+31
label def tod `a' "`i':30", modify
}
forval i = 0(1)23 {
local a = `i'*60+41
label def tod `a' "`i':40", modify
}
forval i = 0(1)23 {
local a = `i'*60+51
label def tod `a' "`i':50", modify
}
label val x1 tod
*********************************************************************
*** creating combined activities for area plot
*********************************************************************
gen sleep = a1
gen personal_care = sleep+a2
gen sex = personal_care + a3
gen eating = sex + a11
gen work = eating + a7
gen education = work + a8
gen housework = education + a4
gen child_care = housework + a5
gen elder_care = child_care + a6
gen shopping = elder_care + a9
gen services = shopping + a10
gen leisure = services + a12
gen TV = leisure + a15
gen travel = TV + a13
gen other = travel + a14
graph twoway (area other travel ///
TV leisure services shopping education ///
work elder_care child_care housework eating sex personal_care ///
sleep x1), ///
xlabel(1 "04:00 am" 250 "6:00 am" 370 "9:00 am" ///
490 "12:00 pm" 670 "15:00 pm" 850 "18:00 pm" ///
970 "20:00 pm" 1090 "22:00 pm", angle(90)) ///
ylabel("") ///
title(An Average Day of an American) ///
ytitle () ///
xtitle (time of the day) ///
legend() scheme(economist)
graph twoway (area a3 x1), ///
xlabel(1 "04:00 am" 250 "6:00 am" 370 "9:00 am" ///
490 "12:00 pm" 670 "15:00 pm" 850 "18:00 pm" ///
970 "20:00 pm" 1090 "22:00 pm" 1210 "00:00 am", angle(90)) ///
ylabel(100 "0.05%" 250 "0.12%") ///
ytitle (Having Sex) ///
xtitle (time of the day) ///
legend() scheme(lean2)
graph twoway (area a6 x1, color(none) lcolor(black)) ///
(area a5 x1, color(none) lcolor(black) lpattern(dash)), ///
xlabel(1 "04:00 am" 250 "6:00 am" 370 "9:00 am" ///
490 "12:00 pm" 670 "15:00 pm" 850 "18:00 pm" ///
970 "20:00 pm" 1090 "22:00 pm" 1210 "00:00 am", angle(90)) ///
ylabel(2000 "1%" 5000 "2.5%" 10000 "5%") ///
title("Eldercare and Childcare in the US") ///
ytitle (Proportion of Population) ///
xtitle (time of the day) ///
legend(label(1 "Eldercare") label(2 "Childcare")) scheme(lean2)