/*Lab Session Week 7 Generating Data with Associations*/ ******************************************************************************** ******************************************************************************** cd "\\ars-data-01\AllUsers_MyDocs\jrodriguezalmaraz\My Documents\Sumer 2018 TA" //modify filepath to your directory clear //Clears all data from memory set more off //so output runs without stopping ****************************************************************************** *Let's create a dataset with age and systolic blood pressure ****************************************************************************** set obs 25 //tells Stata to generate a new empty dataset with 25 observations (rows) gen ID=_n //generates new variable called "ID" which takes on the value of the row set seed 5000 /* when using a random number generator, setting the seed tells the random number generator where to start generating numbers in its sequence. This allows you to generate reproducible results as the random number generator will begin in the same place every time you run the .do file. */ gen age = rnormal(60,5) /*generates "age" a normally distributed random variable with mean=60, standard dev. = 5 */ format age %9.0f // format age to have 0 decimal place //des age sum age // check the mean and standard deviation - notice that it's about what you set it to. gen SBP1= rnormal(99, 1) + 0.65*age /* generate systolic blood pressure, a normally distributed random variable with mean=99, std dev=1. Give it a relationship with age: On average, SBP1 increases by .65 with each year of age.*/ format SBP1 %9.1f // format SBP1 to have 1 decimal place scatter SBP1 age || lfit SBP1 age // visualize the relationship between SBP1 and age regress SBP1 age //regress age on SBP1 and see that it has about the same relationship you specified above. *Now, try changing the specifications and see how each affects your regression results: *Make sample size really small by changing the number in the "set obs" command. clear all set obs 5 gen ID=_n set seed 5000 gen age = rnormal(60,5) format age %9.0f sum age gen SBP1= rnormal(99,1) + 0.65*age format SBP1 %9.1f scatter SBP1 age || lfit SBP1 age regress SBP1 age *Make age association really big by changing .65 to a much larger number. clear all set obs 25 gen ID=_n set seed 5000 gen age = rnormal(60,5) format age %9.0f sum age gen SBP1= rnormal(99,1) + 10*age format SBP1 %9.1f scatter SBP1 age || lfit SBP1 age regress SBP1 age *Make age association really small, but with really big sample size clear all set obs 5000 gen ID=_n set seed 5000 gen age = rnormal(60,5) format age %9.0f sum age gen SBP1= rnormal(99,1) + 0.2*age format SBP1 %9.1f scatter SBP1 age || lfit SBP1 age regress SBP1 age *Set the SBP1 std dev to be really big clear all set obs 25 gen ID=_n set seed 5000 gen age = rnormal(60,5) format age %9.0f sum age gen SBP1= rnormal(99,15) + 0.65*age format SBP1 %9.1f scatter SBP1 age || lfit SBP1 age regress SBP1 age *************************************************************************** *Now, let's create a dataset with SBP measures at 2 timepoints *************************************************************************** clear //Clears all data from memory set more off //so output runs without stopping set obs 25 //tells Stata to generate a new empty dataset with 25 observations (rows) set seed 5000 /* when using a random number generator, setting the seed tells the random number generator where to start generating numbers in its sequence. This allows you to generate reproducible results as the random number generator will begin in the same place every time you run the .do file. */ gen ID=_n //generates new variable called "ID" which takes on the value of the row gen SBP1= rnormal(99, 1) //generate SBP, normally distributed with mean=99, std=1 gen SBP2=SBP1+rnormal(10,1) /*generates "SBP2" a variable that takes on the value of each individual's year 1 SBP plus a change in SBP of mean=10 and std = 1. Under these data generating rules, the mean change in SBP from year one to year two is 10 mmHg with a standard deviation in the change of 1 mmHg.*/ format SBP1 SBP2 %9.2f //format SBP2 with 2 decimal places. //des SBP1 SBP2 ttest SBP2 == SBP1 /*do paired t-test to test if the difference between SBP at time 1 and time 2 is 0.*/ graph box SBP1 SBP2, nooutsides