R:非线性混合效应模型的Nelder-Mead优化

时间:2017-06-26 19:27:15

标签: r optimization mixed-models nonlinear-optimization

找到数据here

library(nlme)
library(dfoptim)
dat0 <- read.table("aids.dat2",head=T)
dat1 <- dat0[dat0$day<=90, ]   # use only first 90-day data
dat2 <- dat1[!apply(is.na(dat1),1,any),]  # remove missing data 
aids.dat <- groupedData(lgcopy ~ day | patid, data=dat2)
aids.dat$log10copy = log10(aids.dat$lgcopy)

myfun2 <- function(s.p1, s.b1, s.p2, s.b2){
  model = nlme(log10copy ~ exp(p1-b1*day) + exp(p2-b2*day + 1),
               fixed = list(p1 ~ 1, b1 ~ 1, p2 ~ 1, b2 ~ 1),
               random = list(patid = pdDiag(list(p1 ~ 1, b1 ~ 1, p2 ~ 1, b2 ~ 1))),
               start = list(fixed = c(p1 = s.p1, b1 = s.b1, p2 = s.p2, b2 = s.b2)),
               data =aids.dat) 
  return(model$logLik)
}

nmkb(fn = myfun2, par = c(1.13, 0.25, 13.1, 1.3), lower = c(0.8, -0.3, 5, -2), upper = c(1.6, 0.6, 20, 13))

我正在尝试运行Nelder-Mead优化算法(在dfoptim包中)以找到我的非线性混合效果模型的良好起始值。我的目标函数是我模型中的logLik。然后我调用nmkb fn是我的函数,par是我的4个参数的值lowerupper界限。但是,我遇到了错误

Error in nlme.formula(log10copy ~ exp(p1 - b1 * day) + exp(p2 - b2 * day +  : 
  argument "s.b1" is missing, with no default  

似乎是一个语法问题,但这里到底出了什么问题?

1 个答案:

答案 0 :(得分:0)

myfun2 <- function(p1b1p2b2){
    s.p1 <- p1b1p2b2[1]
    s.b1 <- p1b1p2b2[2]
    s.p2 <- p1b1p2b2[3]
    s.b2 <- p1b1p2b2[4]
    model = nlme(log10copy ~ exp(p1-b1*day) + exp(p2-b2*day + 1),
                 fixed = list(p1 ~ 1, b1 ~ 1, p2 ~ 1, b2 ~ 1),
                 random = list(patid = pdDiag(list(p1 ~ 1, b1 ~ 1, p2 ~ 1, b2 ~ 1))),
                 start = list(fixed = c(p1 = s.p1, b1 = s.b1, p2 = s.p2, b2 = s.b2)),
                 data =aids.dat) 
    return(model$logLik)
}