非线性混合模型

时间:2015-03-23 12:04:25

标签: r nlme

作为R初学者,我正在尝试修复包含随机因素的模型。公式是:

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

Temp是温度,rate是变化的衡量标准(时间/温度)。为了构造模型并获得初始参数,我使用nlme包:

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`

我也试过这个:

`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"

也许是一个简单的问题,但我还没有找到任何有用的东西。

以下是数据:

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)

提前谢谢!

1 个答案:

答案 0 :(得分:0)

首先,Rate之间存在一些拼写错误rate

其次,我认为该模型在其当前形式中过度参数化,或许尝试类似下面的内容?

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))

第三,我更改了起始值以更好地拟合数据,查看TempRate的图:

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

最后,我猜测了随机效果的结构,我认为?nlmefixed参数的random有一个很好的参考,所以你可以看到你自己如何工作并选择正确的结构。

希望这很有用!