Map function in R for multiple regression -
my goal run multiple regression on each dependent variable in list, using of independent variables in list. store best model each dependent variable aic.
i have written below function guided this post. however, instead of employing each independent variable individually, i'd run model against entire list multiple regression.
any tips on how build function?
dep<-list("mpg~","cyl~","disp~") # list of unique dependent variables ~ indep<-list("hp","drat","wt") # list of first unique independent variables models<- map(function(x,y) step(lm(as.formula(paste(x,paste(y),collapse="+")),data=mtcars),direction="backward"),dep,indep) start: aic=88.43 mpg ~ hp df sum of sq rss aic <none> 447.67 88.427 - hp 1 678.37 1126.05 115.943 start: aic=18.56 cyl ~ drat df sum of sq rss aic <none> 50.435 18.558 - drat 1 48.44 98.875 38.100 start: aic=261.74 disp ~ wt df sum of sq rss aic <none> 100709 261.74 - wt 1 375476 476185 309.45 [[1]] call: lm(formula = mpg ~ hp, data = mtcars) coefficients: (intercept) hp 30.09886 -0.06823 [[2]] call: lm(formula = cyl ~ drat, data = mtcars) coefficients: (intercept) drat 14.596 -2.338 [[3]] call: lm(formula = disp ~ wt, data = mtcars) coefficients: (intercept) wt -131.1 112.5
the y
needs collapsed +
, pasted x
, y
needs passed vector each value of x
models <- lapply(dep, function(x, y) step(lm(as.formula(paste(x, paste(y, collapse="+"))), data=mtcars), direction="backward"), y = indep)
Comments
Post a Comment