[R] how to seperate " "? or how to do regression on each variable when I have multiple variables?

Ista Zahn izahn at psych.rochester.edu
Mon Sep 20 20:49:30 CEST 2010


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



More information about the R-help mailing list