# [R-sig-ME] Confidence Intervals for fitted group lines in nlme model

Bob H mleu@90 @ending from gm@il@com
Wed Aug 22 07:08:07 CEST 2018

```Hi,

I have a npnlinear model y = a*x^b with grouped data (2 groups) and I'd
like to plot the confidence intervals around the fitted lines for each
group. Below you see the fitted lines in the plot.

Can you advise how to do this?

Searching online, there is an answer here:
https://stats.stackexchange.com/questions/231074/confidence-intervals-on-predictions-for-a-non-linear-mixed-model-nlme

that shows how to plot the POPULATION level  (not group-level) confidence
interval in 3 ways (population prediction intervals, bootstrap and delta
method) where there are 14 groups and the overall population fit and
confidence interval is plotted. That is  different than plotting the 14
group confidence intervals around the 14 predicted fits which is what I am
looking to do.

To plot the group-level confidence intervals, for groups A and B in the
example below, how are the random effects, random.effects(m), incorporated
into the confidence interval?

Here is the code:
library(nlme)
library(ggplot2)
set.seed(1)
x = 1:50
y = x^2+rnorm(50,50,50)
x2 = 1:50
y2 = x2^2.2+rnorm(50,50,500)
dat1 = data.frame(x =c(x,x2), y=c(y,y2), group =
c(rep("A",50),rep("B",50))  )

f1<-formula( y ~ a*x^b | group)
#d = groupedData( f1,   data = as.data.frame( dat1 ) )
#### using nlme
m <- nlsList(f1,data=dat1,start=list(a=1,b=1))
m
random.effects(m)
dat1\$fitted = predict(m)

#####plot the predicted lines for each group
ggplot(dat1,aes(x= x, y = y,color = group))+geom_point()+
geom_line(data=dat1, aes(x=x,y=fitted,color=group),inherit.aes=FALSE)

sum(predict(m,level=0)==predict(m,level=1))

Thank you.

library(nlme)

library(brms)

library(ggplot2)

set.seed(1)

x = 1:50

y = x^2+rnorm(50,0,50)

x2 = 1:50

y2 = x2^2.2+rnorm(50,50,500)

dat1 = data.frame(x =c(x,x2), y=c(y,y2), group =
c(rep("A",50),rep("B",50))  )

ggplot(dat1,aes(x= x, y = y,color = group))+geom_point()

f1<-formula( y ~ a*x^b | group)

#d = groupedData( f1,   data = as.data.frame( dat1 ) )

#### using nlme

m <- nlsList(f1,data=d,start=list(a=1,b=1))

m

library(nlme)

library(brms)

library(ggplot2)

set.seed(1)

x = 1:50

y = x^2+rnorm(50,0,50)

x2 = 1:50

y2 = x2^2.2+rnorm(50,50,500)

dat1 = data.frame(x =c(x,x2), y=c(y,y2), group =
c(rep("A",50),rep("B",50))  )

ggplot(dat1,aes(x= x, y = y,color = group))+geom_point()

f1<-formula( y ~ a*x^b | group)

#d = groupedData( f1,   data = as.data.frame( dat1 ) )

#### using nlme

m <- nlsList(f1,data=d,start=list(a=1,b=1))

m

[[alternative HTML version deleted]]

```