0

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

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

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

制作情节:

#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'

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

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

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

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

我想指定级别以获取所有曲线。 R:lme,无法评估“newdata”上所需级别的组 https://stats.stackexchange.com/questions/58031/prediction-on-mixed-effect-models-what-to-do-with-random-effects

4

1 回答 1

0

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

要使绘图代码在问题中正常工作,模型中的随机因子行应该有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应该trialgroup_by图中具有:

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

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

于 2020-06-18T16:07:54.373 回答