试图在R中建立线性回归模型

时间:2019-03-21 03:41:35

标签: r statistics linear-regression

我的数据如下:

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)

如何绘制年份和总人口?

1 个答案:

答案 0 :(得分:2)

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()将线性回归模型拟合到您的数据,插入回归线并插入功能区以显示置信区间。

也许这就是您想要的。

enter image description here

相关问题