I have 3 trials (trial: e1, e2, e3), 2 products/trial (products: A, B), 5 rates/product (.1,1,10,100,1000), total of 6 curves (curve: c1,...,c6). After fitting a non linear mixed model, I want to plot all the curves and the curves resulting from the model in the same chart. This is the reference (package medrc in github): https://doseresponse.github.io/medrc/articles/medrc.html
This is the code to generate the non-linear mixed 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)
To make the 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))
Note that two rows of code are commented. When extracting the predict data per curve, I cannot specify the levels, i.e. the curves that gave the random components. What am I missing?
pdata$resp_ind <- predict(m1, newdata=pdata)
is resulting the error:
Error in predict.nlme(object$fit, newdata = newdata, level = level) :
cannot evaluate groups for desired levels on 'newdata'
So I cannot use this row of code to plot each curve line
geom_line(data=pdata, aes(y=resp_ind), linetype=2) +
These are similar questions, but I'm getting the average trend with the code:
pdata$resp <- predict(m1, newdata=pdata, level=0)
I wanted to specify the levels to get all curves. R: lme, cannot evaluate groups for desired levels on 'newdata' https://stats.stackexchange.com/questions/58031/prediction-on-mixed-effect-models-what-to-do-with-random-effects