【问题标题】:In R, the output of my linear model shows a positive correlation but my ggplot graph indicates a negative correlation?在 R 中,我的线性模型的输出显示正相关,但我的 ggplot 图显示负相关?
【发布时间】:2022-07-21 18:30:36
【问题描述】:

我正在尝试确定 Sycamore_biomass 如何影响鸟类产下第一个蛋的日期。我的模型输出表明存在微弱的正相关关系——即随着美国梧桐生物量的增加,产下第一个蛋的日期应该增加(即应该更晚)(注意我在这个模型中包括了混杂因素):

Call:
lm(formula = First_egg ~ Sycamore_biomass + Distance_to_road + 
    Distance_to_light + Anthropogenic_cover + Canopy_cover, data = egglay_date)

Coefficients:
                    Estimate Std. Error t value Pr(>|t|)  
(Intercept)         39.61055   16.21391   2.443   0.0347 *
Sycamore_biomass     0.15123    0.53977   0.280   0.7851  
Distance_to_road     0.01773    0.46323   0.038   0.9702  
Distance_to_light   -0.02626    0.44225  -0.059   0.9538  
Anthropogenic_cover -0.13879    0.28306  -0.490   0.6345  
Canopy_cover        -0.30219    0.20057  -1.507   0.1628  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 12.99 on 10 degrees of freedom
Multiple R-squared:  0.2363,    Adjusted R-squared:  -0.1455 
F-statistic: 0.6189 on 5 and 10 DF,  p-value: 0.6891

但是,当我使用 ggplot 绘制此图时,回归线表示负关系?谁能帮我解决这里发生的事情?

ggplot(egglay_date, aes(x=Sycamore_biomass, y=First_egg)) +
  geom_point(shape=19, alpha=1/4) +
  geom_smooth(method=lm)

GG PLOT of Sycamore biomass and First egg date

【问题讨论】:

  • 如果您在没有混杂变量的情况下运行线性模型会发生什么?即 lm(First_egg ~ Sycamore_biomass)
  • 我猜其他变量的影响抵消了积极影响。如果您在将所有其他值设置为固定值的同时从模型中预测并绘制了值,您会看到正斜率。基本上,您正在查看由所有系数而不是单个系数引起的数据。

标签: r ggplot2 linear-regression


【解决方案1】:

我想这是因为您查看的是输入模型的原始数据,而不是模型预测。在情节中,您不会“隔离”单个预测变量。您查看所有预测变量对响应变量执行某些操作的结果。我想这个预测器的效果被其他预测器的效果“掩盖”了。

要查看仅一个预测变量的影响,您需要从模型中预测新值,同时修复所有其他预测变量。您可以尝试以下方式:

  preds <- predict(yourmodel, newdata = data.frame(
    "Sycamore_biomass" = 0:25,
    "Distance_to_road" = mean(egglay_date$Distance_to_road),
    "Distance_to_light" = mean(egglay_date$Distance_to_light),
    "Anthropogenic_cover" = mean(egglay_date$Anthropogenic_cover),
    "Canopy_cover" = mean(egglay_date$Canopy_cover)))
  
  new_data <- data.frame(
    "Sycamore_biomass" = 0:25,
    "First_egg" = preds)
  
  ggplot(new_data, aes(x=Sycamore_biomass, y=First_egg)) +
    geom_point(shape=19, alpha=1/4) +
    geom_smooth(method=lm)

仅考虑一个预测变量的影响时,这应该会为您提供模型的预测。

【讨论】:

    猜你喜欢
    • 2019-01-01
    • 2020-04-30
    • 2020-07-23
    • 2021-05-24
    • 1970-01-01
    • 2018-07-06
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多