Created
October 30, 2023 20:48
-
-
Save druedin/3361a3ac4a70c40fbaff87c2e313af5d to your computer and use it in GitHub Desktop.
Example code expanding on https://druedin.com/2015/11/28/same-explanatory-variables-multiple-dependent-variables-in-r/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# library | |
library(lmtest) # library for Breusch-Pagan | |
library(stargazer) # for easy comparion of regression models | |
# create data | |
dv1 = runif(15) | |
dv2 = runif(15) | |
dv3 = runif(15) | |
ev1 = runif(15) | |
ev2 = runif(15) | |
ev3 = as.factor(c(rep("A", 10), rep("B", 5))) | |
mydata = data.frame(dv1, dv2, dv3, ev1, ev2, ev3) | |
# specify outcome variables: | |
dv = c("dv1", "dv2", "dv3") | |
# loop | |
for(i in 1:length(dv)){ | |
model = paste("model",i, sep="") | |
m = lm(as.formula(paste(dv[i],"~ ev1 + ev2")), data=mydata) | |
assign(model,m)} | |
# results | |
stargazer(model1, model2, model3, type="text") | |
# loop with Breusch-Pagan | |
for(i in 1:length(dv)){ | |
bp = paste("bp",i, sep="") | |
m = bptest(lm(as.formula(paste(dv[i],"~ ev1 + ev3")), data=mydata))$statistic | |
assign(bp,m)} | |
# results | |
c(bp1, bp2, bp3) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment