r - Comparing Linear Models with Multiple Interactions -
for assignment, needing determine linear model interaction terms data set , can manually creating different linear models , performing anova test.
what wondering if there function within r check possible combinations of linear model interactions given main effects model? (a function similar same way handles stepwise variable selection)
i've had knowledge of r isn't strong , may have browsed past function.
first column dependent variable(y)(mainclass) , make possible linear regression models.
there 3 independent variables(x,y , z).
mainclass ~ x , mainclass ~ x+y , mainclass ~ y+z , other combinations explored. --code description creating data frame first column mainclass , other columns variable
finding column names excluding first column(mainclass)
having combination of column numbers
having combination of formulas used in linear regression
building linear regression
df<- data.frame(mainclass=1:10,x=rnorm(10,3,1),y=seq(4,40,4),z=seq(100,10,-10)) cols <- names(df) cols <- cols[! cols %in% "mainclass"] comb_id <- unlist(lapply(1:n, function(i)combn(1:n,i,simplify=f)),recursive=f)
formulas <- sapply(comb_id,function(i) paste("mainclass~",paste(cols[i],collapse="+")))
lapply(formulas,function(i) lm(as.formula(i),data=df))
Comments
Post a Comment