简体   繁体   中英

Adding confidence intervals to logarithmic regression

I am trying to add confidence intervals to a logarithmic regression curve in R. I assume there is a mathematical reason that I'm so far unable to do this, so any help would be appreciated.

Here's the data:

Treatment<-c(15,12,6,3,15,12,6,9,9,15,6,9,3,3,12)
CSA<-c(70.32255036, 141.7157414,    185.6793193,    382.1145078,    51.68417543,    
       99.38527948, 164.1865075,    160.8565834,    43.85808722,    46.99992099,
       75.46593836, 116.4039474,    250.3521394,    290.2665555,    122.2660148)
data<-data.frame(Treatment, CSA)

And here's the code to generate the plot

par(mfrow=c(1,1))
par(mar=c(2.5,2.5,1,1))

plot(data$Treatment,data$CSA,ylim=c(0,400),xlim=c(3,15),pch=21,
     xaxt="n",yaxt="n",cex=0.6,xlab=NA,ylab=NA,bty="l")

axis(side=1,tck=-0.02,at=seq(3,15,3),cex.axis=0.6,
     mgp=c(0,0.3,0))
axis(side=2,tck=-0.02,at=seq(0,400,100),cex.axis=0.6,
     las=2,mgp=c(0,.5,0))

ylab<-expression("Total cross-sectional area (cm"^{2}~")")
xlab<-c("Treatment")

mtext(xlab,side=1,line=1.5,cex=0.7)
mtext(ylab,side=2,line=1.5,cex=0.7)

model <- nls(CSA ~ a*log(Treatment)+b,start = 
               list(a = -141,b = 437),data=data)    

xv<-seq(min(data$Treatment),max(data$Treatment),0.5)
yv<-predict(model,list(Treatment=xv))
lines(xv,yv,col="grey23",lwd=1.5)

对数回归图

If I fit a second order polynomial model to the plot using;

mod2<-lm(CSA~poly(Treatment,2),data=data)
xv<-seq(min(data$Treatment),max(data$Treatment),0.5)
yv<-predict(mod2,list(Treatment=xv))
lines(xv,yv,col="grey23",lwd=1.5)

I can add in confidence intervals using;

newx <- seq(min(data$Treatment), max(data$Treatment), length.out=1000)
preds <- predict(mod2, newdata = data.frame(Treatment=newx), 
                 interval = 'confidence')
lines(newx, preds[ ,3], lty = 'dashed', col = "grey36",lwd=1)
lines(newx, preds[ ,2], lty = 'dashed', col = 'grey36',lwd=1)

多项式回归模型

But that doesn't work for the logarithmic curve. Is there a way I can do this in base R?

Thanks in advance

This works fine using lm to fit a linear model to the log-transformed data

mod3 <- lm(CSA ~ log(Treatment),data=data)    
yv<-predict(mod3,list(Treatment=xv))
lines(xv,yv,col="grey23",lwd=1.5)
preds <- predict(mod3, newdata = data.frame(Treatment=newx), 
                 interval = 'confidence')
lines(newx, preds[ ,3], lty = 'dashed', col = "grey36",lwd=1)
lines(newx, preds[ ,2], lty = 'dashed', col = 'grey36',lwd=1)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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