简体   繁体   English

ggeffects:非线性模型的置信区间

[英]ggeffects: confidence interval for non-linear model

I'd to plot the confidence interval for non-linear models using ggeffects package.我想使用 ggeffects 包绘制非线性模型的置信区间。 In my case:就我而言:

#Packages
library(ggplot2) 
library(minpack.lm)  
library(ggeffects)
library(dplyr)

# My dataset
coleo<- read.csv("https://raw.githubusercontent.com/Leprechault/PEN-533/master/coleo-infest-tree.csv")
coleo$DL<-as.numeric(coleo$DL)
coleo$V<-as.numeric(coleo$V)

# My model
m.c.coleo<-nlsLM(V ~ b1 + b2 * I(DL^b3), start = list(b1 = 0.2164, b2 = 1.6264, b3 = 1), data = coleo)
summary(m.c.coleo)

# My plot
ggpredict(m.c.coleo, terms = "DL [all]") %>% plot(add.data = TRUE)

我的情节

The output plot doesn't have a confidence interval and normally the package makes it.输出图没有置信区间,通常包会生成。

Please, any help with it?请问,有什么帮助吗?

I think this is due to the fact that ggpredict uses the generic predict function, and predict.nls does not produce standard errors.我认为这是因为ggpredict使用了通用的predict函数,并且predict.nls不会产生标准错误。 From the docs for ggpredict :来自ggpredict的文档:

ggpredict() uses predict() for generating predictions ggpredict()使用predict()生成预测

And the docs for stats:::predict.nls state: stats:::predict.nls的文档状态:

se.fit A logical value indicating if the standard errors of the predictions should be calculated. se.fit一个逻辑值,指示是否应计算预测的标准误差。 Defaults to FALSE.默认为假。 At present this argument is ignored.目前这个论点被忽略了。

The plot that ggpredict makes for your model seems a bit odd anyway.无论如何, ggpredict为您的模型制作的情节似乎有点奇怪。 If you look at the data, there are only 5 discrete values of DL along the x axis:如果查看数据,沿 x 轴只有 5 个DL离散值:

plot(coleo$DL, coleo$V)

在此处输入图像描述

To make a plot which includes the original points, the nls regression line and a confidence interval for the regression line, you could create one yourself in ggplot.要制作一个包含原始点、nls 回归线和回归线的置信区间的图,您可以在 ggplot 中自己创建一个。 The standard error of the mean at each of the discrete values of DL can be approximated by the standard deviation divided by the square root of the number of points at that x value. DL的每个离散值的平均值的标准误差可以近似为标准差除以该 x 值处的点数的平方根。 A smooth band representing the 95% confidence interval can then be constructed by interpolation:然后可以通过插值构造表示 95% 置信区间的平滑带:

plot_df <- data.frame(
  DL = seq(0, 1, 0.01),
  V = predict(m.c.coleo, newdata = list(DL = seq(0, 1, 0.01))),
  se = 1.96 * summary(m.c.coleo)$sigma / sqrt(approx(sort(unique(coleo$DL)), 
       table(coleo$DL), xout = seq(0, 1, 0.01))$y)
)

ggplot(coleo, aes(DL, V)) + 
  geom_point(size = 2, alpha = 0.5) +
  theme_light() +
  geom_ribbon(data = plot_df, aes(ymin = V - se, ymax = V + se),
              alpha = 0.1) +
  geom_line(data = plot_df)

在此处输入图像描述

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

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