I would like to write a program that automatically plots the means of variables over time for different groups of variables.
Taking a toy data example, let - x_1, x_2 - and - x_3, x_4, x_5 - form variable groups, respectively.
Below is a simplified version of my current program. It works fine if I specify exactly four variables but any number of variables below that makes Stata throw the error "too few variables specified". When specifying any number of variables greater than four, the program stops after plotting the first four variables. So far, I have tried "capture noisily" (see below) but without success.
Any help (also of course on how to write a neater program in general) is appreciated. Thank you very much.
Code:
// Toy data
clear
set seed 8881
set obs 80
egen year = seq(), from(2016) to (2019) block(1)
forvalues i = 1/5 {
gen x_`i' = 1 + `i' + runiformint(1, 5) if year == 2016
replace x_`i' = 2 + `i' + runiformint(1, 5) if year == 2017
replace x_`i' = 3 + `i' + runiformint(1, 5) if year == 2018
replace x_`i' = 4 + `i' + runiformint(1, 5) if year == 2019
label variable x_`i' "Label for variable `i'"
}
// Program
cap program drop plotmean
program define plotmean
syntax varlist
local count: word count `varlist'
forvalues i = 1/`count' {
local x_`i': word `i' of `varlist'
local lab_`i': var label `x_`i''
tempvar x_`i'_mean
egen `x_`i'_mean' = mean(`x_`i''), by(year)
}
capture noisily {
twoway ///
connected `x_1_mean' year, sort ///
|| connected `x_2_mean' year, sort ///
|| connected `x_3_mean' year, sort ///
|| connected `x_4_mean' year, sort ///
legend(order(1 "`lab_1'" 2 "`lab_2'" ///
3 "`lab_3'" 4 "`lab_4'" ))
}
end
0 Response to Program Graph: Allowing for Varying Number of Variables
Post a Comment