繁体   English   中英

How to specify all levels for R{stats} predict() function in non linear mixed model after model fit with package medrc

[英]How to specify all levels for R{stats} predict() function in non linear mixed model after model fit with package medrc

我有 3 次试验(试验:e1、e2、e3)、2 个产品/试验(产品:A、B)、5 个费率/产品(.1、1、10、100、1000),总共 6 条曲线(曲线:c1、 ...,c6)。 在拟合非线性混合 model 后,我想要 plot 的所有曲线和由 model 产生的曲线在同一张图表中。 这是参考(github中的medrc包): https://doseresponse.github.io/medrc/articles/medrc.ZFC35FDC70D5FC69D269883A822C7A5E

这是生成非线性混合 model 的代码。

#packages
library(drc)
library(medrc)
library(dplyr)
library(tidyr)

#my data
trial <- c("e1","e1","e1","e1","e1","e1","e1","e1","e1","e1","e1","e1","e1","e1","e1",
           "e1","e1","e1","e1","e1","e1","e1","e1","e1","e1","e1","e1","e1","e1","e1",
           "e2","e2","e2","e2","e2","e2","e2","e2","e2","e2","e2","e2","e2","e2","e2",
           "e2","e2","e2","e2","e2","e2","e2","e2","e2","e2","e2","e2","e2","e2","e2",
           "e3","e3","e3","e3","e3","e3","e3","e3","e3","e3","e3","e3","e3","e3","e3",
           "e3","e3","e3","e3","e3","e3","e3","e3","e3","e3","e3","e3","e3","e3","e3")

curve <- c("c1","c1","c1","c1","c1","c1","c1","c1","c1","c1","c1","c1","c1","c1","c1",
           "c2","c2","c2","c2","c2","c2","c2","c2","c2","c2","c2","c2","c2","c2","c2",
           "c3","c3","c3","c3","c3","c3","c3","c3","c3","c3","c3","c3","c3","c3","c3",
           "c4","c4","c4","c4","c4","c4","c4","c4","c4","c4","c4","c4","c4","c4","c4",
           "c5","c5","c5","c5","c5","c5","c5","c5","c5","c5","c5","c5","c5","c5","c5",
           "c6","c6","c6","c6","c6","c6","c6","c6","c6","c6","c6","c6","c6","c6","c6")

rates <- c(.1,.1,.1,1,1,1,10,10,10,100,100,100,1000,1000,1000,
           .1,.1,.1,1,1,1,10,10,10,100,100,100,1000,1000,1000,
           .1,.1,.1,1,1,1,10,10,10,100,100,100,1000,1000,1000,
           .1,.1,.1,1,1,1,10,10,10,100,100,100,1000,1000,1000,
           .1,.1,.1,1,1,1,10,10,10,100,100,100,1000,1000,1000,
           .1,.1,.1,1,1,1,10,10,10,100,100,100,1000,1000,1000)

product <- c("A","A","A","A","A","A","A","A","A","A","A","A","A","A","A",
             "B","B","B","B","B","B","B","B","B","B","B","B","B","B","B",
             "A","A","A","A","A","A","A","A","A","A","A","A","A","A","A",
             "B","B","B","B","B","B","B","B","B","B","B","B","B","B","B",
             "A","A","A","A","A","A","A","A","A","A","A","A","A","A","A",
             "B","B","B","B","B","B","B","B","B","B","B","B","B","B","B")

resp <- c(.295,.3232,.3015,.155,.1501,.1483,.0511,.036,.0445,.0021,.0022,.0035,.0015,.0025,.0009,         
      .312,.3373,.2994,.265,.2501,.2482,.1022,.103,.1142,.0220,.0198,.0159,.0036,.0099,.0100,
      .289,.3122,.3093,.141,.1612,.1398,.0722,.022,.0581,.0019,.0015,.0011,.0018,.0009,.0014,
      .325,.3451,.2952,.267,.2412,.2398,.1125,.109,.1019,.0554,.0547,.0118,.0029,.0075,.0078,
      .294,.2452,.2991,.121,.1925,.1485,.0871,.025,.0658,.0019,.0019,.0010,.0025,.0019,.0008,
      .285,.3412,.3069,.124,.1861,.1958,.1276,.132,.1985,.0325,.0201,.0225,.0031,.0089,.0094)


data.test <- data.frame(trial,curve,rates,product,resp) #my data frame

#my model
m1 <- medrm(resp ~ rates, 
            curveid=b + c + d + e ~ product, 
            data = data.test, 
            fct=LL.4(), 
            random = c + d ~ 1|trial,
            start=NULL)

制作 plot:

#plotting
pdata <- data.test%>%
  group_by(curve, product) %>%
  expand(rates=exp(seq(-3, 10, length=50)))
#pdata$resp_ind <- predict(m1, newdata=pdata)
pdata$resp <- predict(m1, newdata=pdata, level=0)

ggplot(data.test, aes(x=log(rates), y=resp, 
                      colour=product, group=curve, shape=product)) +
  geom_point() +
  geom_line(data=pdata) +
  #geom_line(data=pdata, aes(y=resp_ind), linetype=2) +
  theme_bw() +
  scale_x_continuous("DOSE", 
                     breaks=log(c(.1, 1, 10, 100, 1000)), 
                     labels=c(.1, 1, 10, 100, 1000))

请注意,有两行代码被注释掉。 在提取每条曲线的预测数据时,我无法指定级别,即给出随机分量的曲线。 我错过了什么?

pdata$resp_ind <- predict(m1, newdata=pdata)

导致错误:

Error in predict.nlme(object$fit, newdata = newdata, level = level) : 
cannot evaluate groups for desired levels on 'newdata'

所以我不能用这行代码来 plot 每条曲线

geom_line(data=pdata, aes(y=resp_ind), linetype=2) +

这些是类似的问题,但我得到了代码的平均趋势:

pdata$resp <- predict(m1, newdata=pdata, level=0)

我想指定级别以获取所有曲线。 R :lme,无法评估“newdata”上所需级别的组

我可以找出问题所在,然后分享我的发现。

要让 plot 代码按问题中的样子工作,model 中的随机因子行应该有curve而不是trial

    #my model
    m1 <- medrm(resp ~ rates, 
            curveid=b + c + d + e ~ product, 
            data = data.test, 
            fct=LL.4(), 
            random = c + d ~ 1|curve,
            start=NULL)


#plotting
pdata <- data.test%>%
  group_by(curve, product) %>%
  expand(rates=exp(seq(-3, 10, length=50)))
pdata$resp_ind <- predict(m1, newdata=pdata)
pdata$resp <- predict(m1, newdata=pdata, level=0)

ggplot(data.test, aes(x=log(rates), y=resp,
                      colour=product, group=curve, shape=product)) +
  geom_point() +
  geom_line(data=pdata) +
  geom_line(data=pdata, aes(y=resp_ind), linetype=2) +
  theme_bw() +
  scale_x_continuous("DOSE",
                     breaks=log(c(.1, 1, 10, 100, 1000)),
                     labels=c(.1, 1, 10, 100, 1000))

其他有不同随机参数的模型有trial应该在group_by到plot有trial

#my model
m2 <- medrm(resp ~ rates,
            curveid=b + c + d + e ~ product,
            data = data.test,
            fct=LL.4(),
            random = c + d ~ 1|trial/curve,
            start=NULL)

#plotting
pdata <- data.test%>%
  group_by(trial, curve, product) %>%
  expand(rates=exp(seq(-3, 10, length=50)))
pdata$resp_ind <- predict(m2, newdata=pdata)
pdata$resp <- predict(m2, newdata=pdata, level=0)

ggplot(data.test, aes(x=log(rates), y=resp,
                      colour=product, group=curve, shape=product)) +
  geom_point() +
  geom_line(data=pdata) +
  geom_line(data=pdata, aes(y=resp_ind), linetype=2) +
  theme_bw() +
  scale_x_continuous("DOSE",
                     breaks=log(c(.1, 1, 10, 100, 1000)),
                     labels=c(.1, 1, 10, 100, 1000))

使用正确的 model 取决于每种情况,这是另一个主题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM