You have only saved the last run of the for() loop (when i is 4 and j is 
7) to the object called model.  If you want to save all of the lme fits, 
you need to set give some dimensionality to the object model.  For 
example, you could define model as a list, then store the lme fits as 
separate elements of the list.

ids <- a$id
model <- vector("list", 9)
count <- 0
for(i in 2:4){
for(j in 5:7){
        count <- count + 1
        y <- a[, j]
        x <- a[, i]
        model[[count]] <- lme(y ~ x , random= ~1|ids, 
na.action="na.exclude")
        }}

Now you can summarize any of the fits, or get predicted values, or 
whatever ...

summary(model[[5]])
predict(model[[7]])

Jean


arun <smartpink...@yahoo.com> wrote on 07/18/2012 12:34:31 PM:

> Hi Jean,
> 
>  Is there something missing in the function?
> 
> 
> ids <- a$id
> for(i in 2:4){
> for(j in 5:7){
>         y <- a[, j]
>         x <- a[, i]
>         model<-lme(y ~ x , random= ~1|ids, na.action="na.exclude")
>         }}
> 
>  summary(model)
> Linear mixed-effects model fit by REML
>  Data: NULL 
>        AIC      BIC    logLik
>   281.1838 291.5236 -136.5919
> 
> Random effects:
>  Formula: ~1 | ids
>         (Intercept)  Residual
> StdDev:   0.1109054 0.9251637
> 
> Fixed effects: y ~ x 
>                   Value  Std.Error DF    t-value p-value
> (Intercept)  0.03931479 0.09909825 89  0.3967254  0.6925
> x           -0.11826047 0.09731719 89 -1.2152063  0.2275
>  Correlation: 
>   (Intr)
> x 0.056 
> 
> Standardized Within-Group Residuals:
>        Min         Q1        Med         Q3        Max 
> -2.0882452 -0.7718563  0.1156507  0.6119178  1.7986478 
> 
> Number of Observations: 100
> Number of Groups: 10 
> 
> A.K.
> 
> 
> 
> ----- Original Message -----
> From: Jean V Adams <jvad...@usgs.gov>
> To: Berta Ibáñez <bertu...@hotmail.com>
> Cc: Lista de R <r-help@r-project.org>
> Sent: Wednesday, July 18, 2012 1:02 PM
> Subject: Re: [R] fitting several lme sistematically
> 
> I'm not sure why, but lme() doesn't seem to like the variables to be 
> referenced as part of a list using [ or $.
> Here's an easy workaround ...
> 
> ids <- a$id
> for(i in 2:4){
> for(j in 5:7){
>         y <- a[, j]
>         x <- a[, i]
>         lme(y ~ x , random= ~1|ids, na.action="na.exclude")
>         }}
> 
> Jean
> 
> 
> Berta Ibáñez <bertu...@hotmail.com> wrote on 07/18/2012 08:53:51 AM:
> 
> > Dear R-list, 
> > 
> > I have a data set (in the following example called "a") which have: 
> > 
> > one "subject indicator" variable (called "id")
> > three dependent variables (varD, varE, var F)
> > three independent variables (varA, varB, varC)
> > 
> > I want to fit 9 lme models, one per posible combination (DA, DB, DC,
> > EA, EB, EC, FA, FB, FC).
> > In stead of writting the 9 lme models, I want to do it 
> > sistematically (the example is a simplification of what I really 
> > have). Here you have the comands for the first model: 
> > 
> > library(nlme)
> > set.seed(50)
> > a<-data.frame(array(c(rep(1:10,10), rnorm(600)), c(100,7)))
> > names(a)<-c("id", "varA", "varB", "varC", "varD", "varE", "varF")
> > lme(varD ~ varA , random= ~1|id,  data=a, na.action="na.exclude")
> > 
> > I supossed that a simple sintaxis going through the variables of 
> > dataset "a" could cope with it: 
> > 
> > for(i in 2:4){
> > for(j in 5:7){
> > lme(a[,j] ~ a[,i] , random= ~1|id,  data=a, na.action="na.exclude")
> > }}
> > 
> > but it does not, and the use of eval, as.symbol and so on does not 
help. 
> 
> > 
> > for(i in 2:4){
> > for(j in 5:7){
> > lme(eval(as.symbol(names(a)[j])) ~ eval(as.symbol(names(a)[i]))  , 
> > random= ~1|id,  data=a, na.action="na.exclude")
> > }}
> > 
> > Any help??? Thanks a lot in advance!

        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to