【问题标题】:Trying to make a linear regression model in R尝试在 R 中建立线性回归模型
【发布时间】:2019-03-21 03:41:34
【问题描述】:

我的数据如下:

Geographic Area    2000         2001         2002         2003         2004     2005 
Arizona            4779736   4780138         4798834      51689934     5052356

我希望年份是 x 轴,实际值是 y 轴。

我试过了:

x <- seq(2000, 2005, by = 1)
y <- seq(4533372, 4671825, by 10000)

如何绘制年份和总人口?

【问题讨论】:

  • 我不清楚你在问什么。你想model 数据(这是 CIAndrews 似乎已经理解的),还是关于 plotting Geographic Area 与年份?您能否使用例如可重复的格式添加数据? dput?目前,您的数据集的列多于数据。

标签: r statistics linear-regression


【解决方案1】:

Maurits Evers 提出了一个很好的问题。你要模型吗?然后做:

dat <- data.frame("year" = c(2000, 2001, 2002, 2003, 2004),
                   "population" = c(4779736,4780138,4798834,5168993,5052356))

model <- lm(population ~ year, data = dat)

但是你要一个情节,ggplot2 有一个解决方案:

library(ggplot2)

ggplot(aes(x = years, y = population), data = dat) +
  geom_point() +
  geom_smooth(method = "lm")

geom_smooth() 将线性回归模型拟合到您的数据,插入回归线以及显示置信区间的功能区。

也许这就是你要找的东西。

【讨论】:

    最近更新 更多