Create a graph to display observed and fitted values

1k views Asked by At

I am trying to plot the observed and fitted values from a data set and its linear regression against a common time period variable using ggplot.

My data frame is called balances.

gdp.model <- lm(depvar ~ 
              indepvar1 + 
              indepvar2 + 
              indepvar3, 
            data = balances)

gdp.fitted <- ggplot(data = balances,
                 mapping = aes(x = observation.num, y = depvar)) +
                 geom_line() + 
                 geom_line(data = gdp.model,
                           mapping = aes(y = fitted.values))

My intention was to plot the fitted values and observed values on a common axis and the time period variable - observation.num - from balances on the other axis. However, because there is no time period variable in the gdp.model object, I keep getting an error:

Error in eval(expr, envir, enclos) : object 'observation.num' not found

Is there any simple way to get around the issue of the fitted values not having a common time variable?

0

There are 0 answers