[R] All other variables in upper scope arg for stepAIC
David Winsemius
dwinsemius at comcast.net
Thu Oct 21 14:54:15 CEST 2010
On Oct 21, 2010, at 4:21 AM, Paul Chatfield wrote:
> Hi - I am trying to substitute for "the_other_y" in the code below.
> I want
> y2 and y3 to be there when i is 1, y1 and y3 to be there when i is 2
> and y1
> and y2 to be there when i is 3. I'm sure it's to do with what
> format the
> data should be in
No. It's because you expected R to read your mind about creating the
formula. Fortunately the stepAIC function was designed for taking a
character value and turning it into a formula object, or we would also
have needed to use as.formula to create a language object.
> and I've tried alldata[,-i],
Actually all you did was create that matrix but then you never used
it. the lm() function can accept a data= argument but you never used
it, so all the references are to the naked y's.
> but it fits all the columns
> of alldata except i rather than each column one at a time. I've tried
> changing the data i.e. as.matrix or as.list but that's still not the
> right
> format. I'm sure it's a simple trick, but don't know how to search
> it out.
> Any help on how to search this as well as the solution would be
> greatly
> appreciated.
>
> y1<-rnorm(20);y2<-rnorm(20);y3<-rnorm(20);alldata<-cbind(y1,y2,y3)
>
> library(MASS)
> for (i in 1:3)
> {mod<-lm(get(paste("y", i, sep=""))~1)
> stepAIC(mod, scope=list(lower=~1, upper=~the_other_y))}
> library(MASS)
> for (i in 1:3) { the_other_y <- paste("~", paste("y",(1:3)[-i],
sep="", collapse="+"))
+ mod<-lm(get(paste("y", i, sep=""))~1)
+ stepAIC(mod, scope=list(lower=~1, upper= the_other_y))}
Start: AIC=-5.21
get(paste("y", i, sep = "")) ~ 1
Df Sum of Sq RSS AIC
<none> 13.948 -5.2075
+ y3 1 0.166746 13.782 -3.4480
+ y2 1 0.097947 13.850 -3.3484
Start: AIC=3.24
get(paste("y", i, sep = "")) ~ 1
Df Sum of Sq RSS AIC
<none> 21.276 3.2365
+ y1 1 0.149400 21.126 5.0956
+ y3 1 0.025849 21.250 5.2122
Start: AIC=1
get(paste("y", i, sep = "")) ~ 1
Df Sum of Sq RSS AIC
<none> 19.027 1.0023
+ y1 1 0.227457 18.799 2.7618
+ y2 1 0.023117 19.004 2.9780
>
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list