Skip to content

Instantly share code, notes, and snippets.

@Ram-N
Last active March 13, 2016 18:58
Show Gist options
  • Save Ram-N/631999b4ad2caf84b7cc to your computer and use it in GitHub Desktop.
Save Ram-N/631999b4ad2caf84b7cc to your computer and use it in GitHub Desktop.
LM with a few randomly selected columns
one_lm_attempt <- function (df, target) {
rnd5 <- sample(10,5)
predictor_variables <- names(df)[rnd5]
#create the formula, string to give to lm()
rnd_formula <- formula(paste(target, " ~ ",
paste(predictor_variables, collapse=" + ")))
m <- lm(rnd_formula, df)
#print (summary(m)$r.squared)
#print (summary(m)$adj.r.squared)
return(c(rnd5, summary(m)$r.squared,summary(m)$adj.r.squared ))
}
#target <- "string in your data frame that is the name of the dependent variable"
results <- vector()
for (r in 1:10){
results <- c(results, one_lm_attempt(df, target))
}
#Make the Results into a nice data frame
res_df <- data.frame(matrix(results, nrow=10, byrow=T))
names(res_df) = c("c1","c2","c3","c4","c5","R-sq", "AdjRSq")
res_df
> res_df
c1 c2 c3 c4 c5 R-sq AdjRSq
1 6 9 7 8 10 0.7845810 0.7845650
2 2 4 5 1 10 0.8984229 0.8983965
3 2 7 3 9 6 0.7580219 0.7579681
4 10 6 5 7 1 0.8562766 0.8562660
5 2 5 1 10 7 0.8592301 0.8592118
6 8 10 1 3 2 0.8771354 0.8771058
7 4 5 9 3 2 0.7948897 0.7948175
8 3 7 10 8 6 0.7919888 0.7919541
9 10 1 5 8 6 0.8592011 0.8591880
10 3 7 2 6 8 0.7951184 0.7950728
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment