karebear_50
karebear_50

Reputation: 11

Trying to make a linear regression model in R

The data I have is below:

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

I want the years to be the x-axis and the actual values to be the y-axis.

I've tried:

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

How can I plot the year and total population?

Upvotes: 1

Views: 41

Answers (1)

Humpelstielzchen
Humpelstielzchen

Reputation: 6441

Maurits Evers asks a good question. Do you want the model? Then do:

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

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

But you're asking for a plot and there is a solution with ggplot2:

library(ggplot2)

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

geom_smooth() fits a linear regression model to your data, inserts the regression line and also a ribbon to show the confidence intervals.

Maybe this is what you're looking for.

enter image description here

Upvotes: 2

Related Questions