1

As a R beginner, I am trying to fix a model given including a random factor. The formula is:

Temp ~ a - (b * exp(-c *rate))

Where Temp is temperature and rate is a measure of the variation (time/temp). For construct the model and get the initial parameters I use the nlme package:

data1<-groupedData((Temp~rate|Year), data=data)
fm1<-nlme(Temp ~ a - (b * exp(-c *rate)), data=data, fixed=Temp~rate, start=c(a=8.10,b=7.24,c=0.5))
    Error in eval(expr, envir, enclos) : object 'a' not found`

I have tried this as well:

`fm100<-selfStart(~a-(b*exp(-c*rate)),
function(mCall, data, LHS)
{
xy<-sortedXyData(mCall[["x"]], LHS, data)
tmp<-coef(lm(Temp~rate, data=data),
value<-c(exp(tmp[1],temp[2])
getInitial=c("a","b","c"))
}`

Error: unexpected symbol in:
"value<-c(exp(tmp[1],temp[2]) getInitial"

Perhaps is a simple question but I haven’t found anything useful yet.

Here are the data:

Temp<-c(9,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,14,14,17.1,17.6,17.6,19.6,20.6,21.3,21.3,22.8,22.8,22.8,22.8,22.8,22.8,22.8,22.8,22.8,22.8,22.8,23.5,23.8,11.4,11.4,11.4,11.4,11.7,11.7,12.6,13.6,13.6,14.6,14.6,14.6,14.6,15.7,15.7,15.7,16.1,16.1,16.7,16.7,11.6,12.6,12.6,12.6,14.5,14.5,14.7,15.8,15.8,15.8,15.8,16,16,16,16,16,16,16,16,16,16)
Rate<-c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
Year<-c(2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011) 
data<-data.frame(Temp, Rate, Year)

Thank you in advance!

dej
  • 11
  • 2
  • Please check your code for typos, as well as unmatched parentheses! (restart your R session and rerun your code, you'll see what I mean) – Dominic Comtois Mar 23 '15 at 12:38
  • Thank you Dominic. I have corrected the errors but it doesn't help me to fix the model. – dej Mar 23 '15 at 13:56

1 Answers1

0

Firstly, there are a few typos Rate between rate.

Secondly, I think the model is over-parameterised in it's current form, perhaps try something like the following instead?

library(nlme)
Temp <- c(9,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,14,14,17.1,17.6,17.6,19.6,20.6,21.3,21.3,22.8,22.8,22.8,22.8,22.8,22.8,22.8,22.8,22.8,22.8,22.8,23.5,23.8,11.4,11.4,11.4,11.4,11.7,11.7,12.6,13.6,13.6,14.6,14.6,14.6,14.6,15.7,15.7,15.7,16.1,16.1,16.7,16.7,11.6,12.6,12.6,12.6,14.5,14.5,14.7,15.8,15.8,15.8,15.8,16,16,16,16,16,16,16,16,16,16)
Rate <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,0.0417,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
Year <- c(2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2006,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011) 
data <- data.frame(Temp, Rate, Year)

fm1 <- nlme(Temp ~ a * exp(-b * Rate), 
    data = data, 
    fixed = a + b ~ 1, 
    random = a + b ~ 1 | Year, 
    start = c(a = 20.0, b = 0.5))

Thirdly, I changed the starting values to actually fit the data a little better, look at a plot of Temp against Rate:

f <- function(Rate) {
    a <- 20.0
    b <- 0.5
    a * exp(-b * Rate)
}

plot(Temp ~ Rate, data = data)
curve(f, add = TRUE, lwd = 2, col = "red")

plot to help with starting values

Lastly, I have taken a guess at the structure of your random effects, I think a good reference is in ?nlme for the fixed and random arguments, so you can see for yourself how these work and choose the correct structure.

Hope this is useful!

Jeff
  • 718
  • 8
  • 20
  • Thank you for the answer Jeff. In fact, the model you propose is my second model (next I will choose the better one by AIC). I tried to do the same using the first model but I get this error: > fm2<-nlme(Temp ~ a - (b * exp(-(c *Rate))), + data=data, + fixed=a+b+c~1, + random=a+b+c~1 | Year, + start = c(a = 20.0, b = 5, c =0.5)) Error en MEEM(object, conLin, control$niterEM) : Singularity in backsolve at level 0, block 1 – dej Mar 23 '15 at 14:04
  • Yes I did exactly the same, and got the same error, which was why I then realised the model was over-parameterised. – Jeff Mar 23 '15 at 14:15