r - 在 R 中绘制回归输出时指定不同组的终点

标签 r regression linear-regression ggplot2

我希望在为我的硕士论文呈现回归输出方面获得一些帮助。我正在评估大象对木本植被的影响,特别是与人工水坑有关的影响。除了随着距水坑距离的增加而普遍减弱之外,所涉及的两种植被类型之间的影响也存在很大差异。

我已经找到了使用 visreg 绘制此图的令人满意的方法。在下面显示的模型输出中,到水坑的距离和蔬菜类型都解释了损坏,因此我尝试同时显示这两者。然而,问题是我只有距离红色植被类型最远的水坑(x 轴)处的样本。正如您所看到的,蓝色蔬菜类型的回归线超出了该植被类型的最后一个点。无论如何,我是否可以让蓝线停在距水坑(x 轴值)更小的距离处,而不是红线,以避免这种情况?

请参阅 visreg 图下方的模型和图代码。

Damage to vegetation (y axis) against distance to waterhole (currently scaled, hence the funny units). The two colours correspond to the two vegetation types

示例数据和代码

> dput(vegdata[21:52, c(4,7,33)])
structure(list(distance = c(207L, 202L, 501L, 502L, 1001L, 1004L, 
2010L, 1997L, 4003L, 3998L, 202L, 194L, 499L, 494L, 1004L, 1000L, 
2008L, 1993L, 4008L, 3998L, 493L, 992L, 1941L, 2525L, 485L, 978L, 
1941L, 3024L, 495L, 978L, 1977L, 2952L), vegtype = structure(c(1L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label =  c("teak", 
"term"), class = "factor"), toedl = c(35.48031025, 47.30482718, 
25.16709533, 22.29360164, 17.6546533, 12.81605101, 20.34136734, 
18.45809334, 11.3578081, 3.490830751, 60.54870317, 44.9863128, 
18.81010698, 20.4777188, 30.36994386, 18.7417214, 21.52247156, 
18.29685939, 30.26217664, 8.945486104, 43.95749178, 43.54799495, 
44.42693993, 50.06207783, 48.05538594, 35.31220933, 52.37339094, 
40.51569938, 41.45677007, 58.86629306, 37.80203313, 46.35633342
)), row.names = 21:52, class = "data.frame")

m1<-lm(toedl~vegtype+distance, data=vegdata)
summary(m1)

library(visreg)
visreg(oedl6, 'sexactd', by='vegtype',overlay=TRUE, gg=TRUE,    points=list(size=2.5), ylab='% old elephant damage', xlab='distance from waterhole')

最佳答案

关于可重现示例的注释,您可以制作一个带有代表性数据的小数据框,如下所示,也是一般注释,您应该避免将基本函数的变量名称命名为“all”。

我不确定是否可以使用 visreg 来做你想做的事情,但你可以使用预测从模型中提取信息,然后使用 ggplot 来绘制它,这可能更好,因为 ggplot 非常适合自定义地 block 。

library(ggplot2)
library(visreg)

# Create reproducible data example
allData <- data.frame(vegtype = rep(c("t1", "t2"), each = 10),
                      oedl = c(seq(from = 35, to = 20, length.out = 10),
                               seq(from = 20, to = 5, length.out = 10)),
                      sexactd = c(seq(from = -1, to = 1, length.out = 10),
                                  seq(from = -1, to = 2, length.out = 10)))

# Make linear model
oedl6 <- lm(formula = oedl ~ sexactd + vegtype, data = allData)

# Predict the data using the linear model
odelPred <- cbind(allData, predict(oedl6, interval = 'confidence'))

ggplot(odelPred, aes(sexactd, oedl, color = vegtype, fill = vegtype)) +
  geom_point() + geom_line(aes(sexactd, fit)) + 
  geom_ribbon(aes(ymin = lwr, ymax = upr), alpha = 0.3)

关于r - 在 R 中绘制回归输出时指定不同组的终点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59312025/

相关文章:

r - For 循环 - 从日列中选择时间窗口

r - 使用 data.table 为行 block 创建组标签

machine-learning - 使用 TensorFlow 进行非线性回归,结果呈直线

r - 分段包中的错误 : breakpoints confusion

R 绘制积分

RStudio:git add --all 来自 UI

r - 如何在R中拟合信息(负熵)〜大小的回归?

machine-learning - 房屋 build 年份: regression or classification?的图像分类

python - 如何从 statsmodels.api 中提取回归系数?

r - 通过变量列表为 R 中的 LM 模型创建循环