首页 文章

如何在ggplot2 qplot上叠加修改过的黄土线?

提问于
浏览
9

背景

现在,我正在创建一个多预测线性模型并生成诊断图来评估回归假设 . (这是我目前喜欢的多元回归分析统计课程:-)

我的教科书(Cohen,Cohen,West和Aiken 2003)建议根据残差绘制每个预测因子,以确保:

  • 残差不系统地与预测变量共同

  • 残差与模型中的每个预测变量相同

在第(2)点,我的教科书有这样的说法:

一些统计软件包允许分析师在残差平均值(0线),高于平均值的1个标准差和低于残差平均值的1个标准偏差下绘制低值拟合线....在本案例中{他们的例子},两条线{mean 1sd和mean - 1sd}大致平行于lowess {0}线,与残差的方差不随X变化的解释一致 . (p.131)

如何修改黄土线?

我知道如何使用“0行”生成散点图:

# First, I'll make a simple linear model and get its diagnostic stats
    library(ggplot2)
    data(cars)
    mod <- fortify(lm(speed ~ dist, data = cars))
    attach(mod)
    str(mod)

    # Now I want to make sure the residuals are homoscedastic
    qplot (x = dist, y = .resid, data = mod) + 
    geom_smooth(se = FALSE) # "se = FALSE" Removes the standard error bands

但有没有人知道如何使用 ggplot2qplot 来生成0线,"mean + 1sd"和"mean - 1sd"线叠加的图?这是一个奇怪/复杂的问题吗?

3 回答

  • 1

    道歉

    伙计们,我想为我的无知道歉 . 哈德利是绝对正确的,答案就在我面前 . 我怀疑,我的问题源于统计,而不是程序化的无知 .

    我们免费获得68%的置信区间

    geom_smooth() 默认为 loess 平滑,它将1sd和-1sd行叠加作为交易的一部分 . 那个68%的间隔是什么,并且一直在寻找我已经知道该怎么做的东西 . 它没有通过指定 geom_smooth(se = FALSE) 来实际关闭我的代码中的置信区间 .

    我的示例代码应该看起来像什么

    # First, I'll make a simple linear model and get its diagnostic stats.
    library(ggplot2)
    data(cars)
    mod <- fortify(lm(speed ~ dist, data = cars))
    attach(mod)
    str(mod)
    
    # Now I want to make sure the residuals are homoscedastic.
    # By default, geom_smooth is loess and includes the 68% standard error bands.
    qplot (x = dist, y = .resid, data = mod) + 
    geom_abline(slope = 0, intercept = 0) +
    geom_smooth()
    

    我学到了什么

    哈德利实施了一种非常美丽而简单的方式来获得我一直想要的东西 . 但由于我专注于黄土线,我忽略了这样一个事实:68%的置信区间受到了我需要的线条的限制 . 对不起,大家好 .

  • 4

    你能从数据计算/ - 标准偏差值,并将它们的拟合曲线添加到图中吗?

  • 1

    看看我的问题“modify lm or loess function..

    我不确定我是否非常关注你的问题,但也许是:

    + stat_smooth(method=yourfunction)
    

    如果您将函数定义为described here,则可以正常工作 .

相关问题