Skip to content

Instantly share code, notes, and snippets.

@druedin
Created October 30, 2023 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save druedin/3361a3ac4a70c40fbaff87c2e313af5d to your computer and use it in GitHub Desktop.
Save druedin/3361a3ac4a70c40fbaff87c2e313af5d to your computer and use it in GitHub Desktop.
# 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