-1

Hi I want to perform several regressions under different conditions. I have achieved to do this successfully but when I get the list with all coefficients, the names of the levels are missing whenever I use more than one level factor. However, if I only use one factors names remain .

What I want is calculate all parameters under two conditions and obviously get the names. I want to do this via looping because my data is much more complex than this.

library(bear)

 model<-function(A,mu) {
   A+mu*time
 }

mod  <- ln_rel.cc ~A+mu*time
rep<- c("r1","r2","r3","r4","r5")
Medium<-c("A","B")
time<-c(seq(from=0, to=39,by=1))
ln_rel.cc<-model(A=1,mu=0.4)
MedTest<-cbind(rep,Medium,time,ln_rel.cc )
MedTest<-data.frame(MedTest)
MedTest$time<-as.numeric(MedTest$time)
MedTest$ln_rel.cc<-as.numeric(MedTest$ln_rel.cc)
head(MedTest)
values<-NULL
##one factor regression
for(M in levels(MedTest$Medium)){


    values[M]<-list((summary(lm(ln_rel.cc ~time, MedTest[ MedTest$Medium==M ,]
    ))$coef))



}
values
values<-NULL
##Two factors regression

for(M in levels(MedTest$Medium)){

  for (R in levels(MedTest$rep)){

    values[M[R]]<-list((summary(lm(ln_rel.cc ~time, MedTest[MedTest$rep==R & MedTest$Medium==M ,]
                                     ))$coef))


  }
}
values

Thanks for the help

Santi
  • 368
  • 2
  • 15
  • 1
    Please [read about how to provide a **minimal** reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Show us the specific problem, perhaps by eliminating the for loops. Intuition tells me the process of breaking down your code to identify the problem will probably reveal the answer to you. – Thomas Mar 20 '15 at 11:56

1 Answers1

0

I have found the way to answer this problem!!

Generate some empty vectors

values<-NULL
mutation<-NULL
Inoc.size<-NULL
rep<-NULL

Create a for loop with three levels and three independent vector in which store what variable is being used at each loop

library(grofit)
for(M in levels(Datos$mutation)){

  for (I in levels(Datos$Inoc.size)){
    for(R in levels(Datos$rep)){
    x <- Datos%>%
      select(ln_rel.cc,Inoc.size, mutation, rep, time )%>%
      filter(mutation==M&
             Inoc.size==I&
             rep==R)


    values[M[I]] <-list(summary(gcFitModel(time=x$time, data=x$ln_rel.cc )))  
    for (i in length(values)){
      mutation[i]<-c(M)
      Inoc.size[i]<-c(I)
      rep[i]<-c(R)

    }
    }
}
}

Join the data creating a logic data frame

##data frame
parameters<-NULL
parameters<-do.call(rbind, lapply(values, function(X) X[] ))
parameters$mutation<-mutation
parameters$Inoc.size<-Inoc.size
parameters$rep<-rep
parameters<-parameters%>%
  select(mutation,Inoc.size,rep, mu.model, lambda.model,A.model,
         stdmu.model,stdlambda.model,stdA.model)
parameters<-rename(parameters,
                   c("mu.model"="mu", "lambda.model"="l", "A.model"="A",
                     "stdlambda.model"="sigma.l"))
Santi
  • 368
  • 2
  • 15