【问题标题】:ggplot printing regression equations where there are multiple lm on one plotggplot 打印回归方程,其中一张图上有多个 lm
【发布时间】:2015-10-06 10:46:15
【问题描述】:

这是该图中使用的我的数据框中的数据子集;

Year    region  gear    Species.Code    query   LPUE
1974    Cyprus  creel   LOB             Pre     0.31
1975    Cyprus  creel   LOB             Pre     0.26
1976    Cyprus  creel   LOB             Pre     0.33
1977    Cyprus  creel   LOB             Pre     0.17
1978    Cyprus  creel   LOB             Pre     0.2
1979    Cyprus  creel   LOB             Pre     0.22
1980    Cyprus  creel   LOB             Pre     0.38
1981    Cyprus  creel   LOB             Pre     0.51
1982    Cyprus  creel   LOB             Pre     0.57
1983    Cyprus  creel   LOB             Pre     0.45
1984    Cyprus  creel   LOB             Post    0.43
1985    Cyprus  creel   LOB             Post    0.33
1986    Cyprus  creel   LOB             Post    0.21
1987    Cyprus  creel   LOB             Post    0.69
1988    Cyprus  creel   LOB             Post    0.65
1989    Cyprus  creel   LOB             Post    0.37
1990    Cyprus  creel   LOB             Post    0.35
1991    Cyprus  creel   LOB             Post    0.15
1992    Cyprus  creel   LOB             Post    0.21
1993    Cyprus  creel   LOB             Post    0.17

我已经生成了时间序列数据的线图,并拟合了两个线性回归,一个用于 1984 年之前的数据,一个用于 1984 年之后的数据。 使用以下代码;

ggplot(subset(A7,region=="Cyprus"&gear=="creel"&Species.Code=="LOB"),aes(x=Year,y=LPUE,shape=query))+
  geom_line()+
  geom_smooth(method="lm")+
  theme(panel.background = element_rect(fill = 'white', colour = 'black'))

首先我想知道如何在绘图上打印方程式(我在堆栈溢出中发现的其他示例仅处理一个 lm),其次我想知道如何保存 lm 模型以便我可以进行统计测试他们之间的意义。

【问题讨论】:

  • 您拥有的内容的屏幕截图会有所帮助,因为您可能没有包含足够的数据来重现您拥有的内容。
  • 如果你想做统计测试,我建议在 ggplot 之外拟合模型。
  • 抱歉@MikeWise 我不熟悉如何添加屏幕截图。我只能看到如何添加来自互联网的图像。

标签: r ggplot2 regression


【解决方案1】:

您可以通过堆叠 geom_smooth 命令将几条回归线添加到 ggplot。我建议为您想要拟合的两个模型创建单独的数据框。

data_upto84 <- subset(A7, year<1985)
data_from84 <- subset(A7, year>1984)

ggplot(data_upto84, aes(YEAR, LPUE)) + 
stat_summary(fun.data=mean_cl_normal) +
geom_smooth(method = 'lm') +
geom_smooth(data = data_from84, aes(YEAR, LPUE), method = 'lm')

关于您的第二个问题:“其次,我如何保存 lm 模型,以便对它们之间的显着性进行统计检验”。 通过将模型分配给对象来保存模型:

model1 <- lm(LPUE ~ 1 + YEAR, data = data_upto84)
model2 <- lm(LPUE ~ 1 + YEAR, data = data_from84)

我不清楚你想用这些模型测试什么。当您在两个不同的样本上运行模型时,标准模型比较(例如使用 anova 函数)将无效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多