******************************************************************************** ** Biostat 212 Summer 2020 Week 3 code ** Created 8/5/2020 by Amanda Irish ** Input file: raw acupuncture data - csv file ** Output file: smcl log file ******************************************************************************** version 16.1 capture log close cd "/Users/amandairish/Desktop/Biostat 212/Week 3" log using lecture3log, replace * Import the raw data import delimited acupuncture_raw.csv, clear * Rename & label variable f1 rename f1 hfreq_bl label variable hfreq_bl "baseline headache frequency" * Label values of group variable tab group // to see what it looks like before labeling label define grouplabel 0 "control" 1 "intervention" label values group grouplabel tab group // to see what it looks like after labeling * Generate a new running ID variable according to current row number gen newid = _n /* note that the Stata-generated variable _n keeps track of the current row number and does not correspond uniquely to a particular observation - important if you sort your data */ gsort +id // sort id variable in ascending order gen newrow = _n // generate a new variable based on row number after sorting browse id newid newrow /* notice how newid and newrow are different because of sorting on id */ * Generate a new blank variable (all values set to missing) gen blank = . browse newid blank // examine new variables * Generate a new indicator variable "severe migraine" for those with a * migraine and a headache score >50 gen severemigraine = (migraine==1 & basehs>50) tab severemigraine // examine new variable * Generate a new categorical variable for quartiles of baseline headache score xtile hs_quartile = basehs, nq(4) * What is the mean baseline headache score in the first quartile? bysort hs_quartile: sum basehs * Create a variable for average headache score that is the mean of 3 other * headache score variables: basehs, posths, and oneyrhs egen meanhs = rowmean(basehs posths oneyrhs) browse basehs posths oneyrhs meanhs // examine new variable * Generate age a dichotomous variable for age (>50) inspect age // check how age is coded (whole numbers or decimels) recode age (min/50 = 0 "less than 51") (51/max = 1 "51 and older"), gen(age2) * How many study participants are 51 years and older? tab age2 * Are there any outliers for age? spikeplot age sum age list if age >120 * Replace outlier replace age = 52 if id==363 // OR replace age = 52 if age >115 spikeplot age // check to make sure outlier has been taken care of * Convert withdrawal_reason from string to a categorical numeric variable codebook withdrawal_reason // how is it currently coded? encode withdrawal_reason, gen(dropout) // use encode since it's categorical codebook dropout // examine new variable * What percent of individuals who dropped out withdrew consent? tab dropout ************** Other commands from lecture 3 **************** recode completed (-99 = .) label data "This dataset contains raw acupuncture trial data" numlabel, add // add numerical prefixes to categorical variable labels tab group // see how value displays differently labelbook // look at all value labels attached to the dataset log close