Linear vs Quadratic Fit

Author

AS

Raw Data

remove(list=ls())
library(stargazer)

Please cite as: 
 Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
 R package version 5.2.3. https://CRAN.R-project.org/package=stargazer 
set.seed(123)

x <- seq(from = -5, 
         to = 5, 
         length.out = 100)

y <- 2 + 0.5 * x - 0.3 * x^2 + rnorm(100, sd = 2)  # true quadratic relationship + noise

#y <- 2 + 0.5 * x - 0.3 * x^2 + 1 * x^3 - 0.7 * x^4 + 8 * x^5 + 1 * x^6 - 0.7 * x^7 + 8 * x^8 + rnorm(100, sd = 2)  # true quadratic relationship + noise


df <- data.frame(x = x, y = y)

remove(x)
remove(y)

Linear Model (Simple Linear Regression)

model_linear <- lm(formula = y ~ x,
                   data = df
                   )

# model_linear <- lm(formula = df$y ~ df$x)

plot(model_linear)

Quadratic Model (Still Simple Regression, but Nonlinear in x)

model_quad <- lm(y ~ x + I(x^2), 
                 data = df)

plot(model_quad)

Polynomial Model (Simple Regression, but Nonlinear in x)

model_poly <- lm(y ~ x + I(x^2) + I(x^3) + I(x^4) + I(x^5) + I(x^6) + I(x^7), 
                 data = df)

plot(model_poly)


stargazer(model_linear, model_quad, model_poly,
          type = "text")

=========================================================================================
                                             Dependent variable:                         
                    ---------------------------------------------------------------------
                                                      y                                  
                             (1)                     (2)                    (3)          
-----------------------------------------------------------------------------------------
x                          0.550***               0.550***                 0.283         
                           (0.096)                 (0.063)                (0.413)        
                                                                                         
I(x2)                                             -0.275***              -0.635***       
                                                   (0.024)                (0.190)        
                                                                                         
I(x3)                                                                      0.054         
                                                                          (0.117)        
                                                                                         
I(x4)                                                                      0.038*        
                                                                          (0.020)        
                                                                                         
I(x5)                                                                      -0.003        
                                                                          (0.010)        
                                                                                         
I(x6)                                                                     -0.001*        
                                                                          (0.001)        
                                                                                         
I(x7)                                                                     0.00003        
                                                                          (0.0002)       
                                                                                         
Constant                    -0.370                1.971***                2.482***       
                           (0.279)                 (0.274)                (0.401)        
                                                                                         
-----------------------------------------------------------------------------------------
Observations                 100                     100                    100          
R2                          0.252                   0.682                  0.697         
Adjusted R2                 0.244                   0.675                  0.674         
Residual Std. Error    2.789 (df = 98)         1.829 (df = 97)        1.833 (df = 92)    
F Statistic         33.023*** (df = 1; 98) 103.930*** (df = 2; 97) 30.195*** (df = 7; 92)
=========================================================================================
Note:                                                         *p<0.1; **p<0.05; ***p<0.01

Plot fit

library(ggplot2)

ggplot(data = df, 
       mapping = aes(x = x, 
                     y = y)
       ) +
  geom_point(color = "grey40", alpha = 0.6) +
  stat_smooth(method = "lm", 
              formula = y ~ x, 
              se = FALSE, 
              color = "blue", 
              linetype = "dashed") +
  stat_smooth(method = "lm", 
              formula = y ~ x + I(x^2), 
              se = FALSE, 
              color = "red", size = 1.2) +
    stat_smooth(method = "lm", 
              formula = y ~ x + I(x^2) + I(x^3) + I(x^4) + I(x^5) + I(x^6) + I(x^7), 
              se = FALSE, 
              color = "pink", size = 1.2) +
  labs(title = "Straight Line vs Quadratic Fit vs Polynomial Fit" ,
       subtitle = "Blue dashed = Linear model, Red = Quadratic model",
       x = "x", y = "y")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.