Soyeon Kim
2010-Sep-20 17:03 UTC
[R] how to seperate " "? or how to do regression on each variable when I have multiple variables?
Dear All, I have data which contains 14 variables. And I have to regress one of variables on each variable (simple 13 linear regressions) I try to make a loop and store only R-squared colnames(boston) [1] "CRIM" "ZN" "INDUS" "CHAS" "NOX" "RM" "AGE" [8] "DIS" "RAD" "TAX" "PTRATIO" "B" "LSTAT" "MEDV" name <- colnames(boston) r <- rep(0, 13) for(i in 1: 13) { r[i] <- summary(lm(MEDV ~ name[i], data = boston))$r.squared } but this doesn't work because name have " " for each variable. How to remove " " for name of each variable? Or do you know the way I can do regression MEDV on each variable? Thank you ahead, Soyeon
Henrique Dallazuanna
2010-Sep-20 18:33 UTC
[R] how to seperate " "? or how to do regression on each variable when I have multiple variables?
Try this: lapply(names(boston), function(x)summary(update(lm(MEDV ~ 1, boston), ~ get(x)))) On Mon, Sep 20, 2010 at 2:03 PM, Soyeon Kim <yunni0731@gmail.com> wrote:> Dear All, > > I have data which contains 14 variables. And I have to regress one of > variables on each variable (simple 13 linear regressions) > > I try to make a loop and store only R-squared > > colnames(boston) > [1] "CRIM" "ZN" "INDUS" "CHAS" "NOX" "RM" "AGE" > [8] "DIS" "RAD" "TAX" "PTRATIO" "B" "LSTAT" "MEDV" > > name <- colnames(boston) > > r <- rep(0, 13) > for(i in 1: 13) { > r[i] <- summary(lm(MEDV ~ name[i], data = boston))$r.squared > } > > but this doesn't work because name have " " for each variable. How to > remove " " for name of each variable? > Or do you know the way I can do regression MEDV on each variable? > > Thank you ahead, > Soyeon > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Ista Zahn
2010-Sep-20 18:49 UTC
[R] how to seperate " "? or how to do regression on each variable when I have multiple variables?
Hi Soyeon, Here are a few options: ## Use get() to find the predictor r <- rep(0, 13) for(i in 1: 13) { r[i] <- summary(lm(MEDV ~ get(name[i]), data = boston))$r.squared } ## Use as.formula(paste()) to construction the model for(i in 1: 13) { r[i] <- summary(lm(as.formula(paste("MEDV ~ ", name[i], sep="")), data = boston))$r.squared } ## Just square the correlations (this is how I would do it) r <- cor(boston)["MEDV",]^2 Best, Ista On Mon, Sep 20, 2010 at 1:03 PM, Soyeon Kim <yunni0731 at gmail.com> wrote:> Dear All, > > I have data which contains 14 variables. And I have to regress one of > variables on each variable (simple 13 linear regressions) > > I try to make a loop and store only R-squared > > ?colnames(boston) > ?[1] "CRIM" ? ?"ZN" ? ? ?"INDUS" ? "CHAS" ? ?"NOX" ? ? "RM" ? ? ?"AGE" > ?[8] "DIS" ? ? "RAD" ? ? "TAX" ? ? "PTRATIO" "B" ? ? ? "LSTAT" ? "MEDV" > > name <- ?colnames(boston) > > r <- rep(0, 13) > for(i in 1: 13) { > ?r[i] <- summary(lm(MEDV ~ name[i], data = boston))$r.squared > } > > but this doesn't work because name have " " for each variable. How to > remove " " for name of each variable? > Or do you know the way I can do regression MEDV on each variable? > > Thank you ahead, > Soyeon > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org