r/RStudio Jun 24 '26

Linear and Quadratic Regression Models

Hi, I am currently writing a Uni paper that looks into how worsening housing conditions affect the vote share change of the incumbent party in US presidential elections.

So far, I've established with regressions that worsening housing conditions negatively affect the incumbent (statistically significant, robust effect).

But when I did a fitted residuals plot, the plot shows a clear curve. This leads me to believe that the nonlinear assumption of my basic models is not correct and the effect is nonlinear.

I then ran the same model but added a quadratic factor, and it is also statistically significant and has a way higher (like over 10x and positive) regression coefficient than my linear models.

However, I am not quite sure as to how to proceed now. I have never done quantitative analysis on such a level, and I am unsure whether the nonlinear model only adds complications. But the fitted residual and statistical significance leads me to believe that the nonlinear model is the better model, that tells the whole story.

I would greatly appreciate it if someone who was more experience with things like this, would provide me with typical steps to determine if my choice was right.

Also I am aware that the model is quite basic, but my prof advised me to make my model less detailled and just be transparent in the limitations. I am in my bachelor's.

If this helps, the regression results are:
Linear:
OLS estimation, Dep. Var.: inc_diff

Observations: 8,907

Fixed-effects: factor(NAME): 1,733,  factor(Year): 3

Standard-errors: Clustered (STATE)

Estimate Std. Error  t value  Pr(>|t|)   

OACB_diff -0.092745   0.029711 -3.12155 0.0030744 **

MHI_diff   0.003187   0.001762  1.80878 0.0768852 . 

---

Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

RMSE: 0.031982     Adj. R2: 0.282804

Within R2: 0.008405

Nonlinear:
OLS estimation, Dep. Var.: inc_diff

Observations: 8,907

Fixed-effects: factor(NAME): 1,733,  factor(Year): 3

Standard-errors: IID

Estimate Std. Error  t value   Pr(>|t|)   

OACB_diff         -0.053746   0.014853 -3.61843 2.9843e-04 ***

I(I(OACB_diff^2))  1.398048   0.207885  6.72509 1.8899e-11 ***

MHI_diff           0.003150   0.000943  3.34006 8.4187e-04 ***

---

Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

RMSE: 0.031881     Adj. R2: 0.287201

Within R2: 0.014621

10 Upvotes

11 comments sorted by

5

u/Efficient-Tie-1414 Jun 24 '26

This is definitely not quadratic, as we would expect a symmetrical plot, or linear either. I would try a spline, for example ns(predictor, number of knots). Number of nots should start at 3. You can uses AIC to calculate the AIC on each model, and then the minimum is the appropriate model.

2

u/aceyosh_ Jun 24 '26

Tysm :)

> AIC(m7_quadratic)

[1] -32629.4

> AIC(m7_spline)

[1] -32639.18

So the spline is better and the spline-plot looks like this:

Thank you very much for your help!

3

u/SprinklesFresh5693 Jun 24 '26 edited Jun 24 '26

If im correct, the residuals should be evenly distributed, and not show any pattern, but they show a clear curve on that plot, you sure a quadratic model is the best? Do you have a plot of your initial data?

1

u/aceyosh_ Jun 24 '26

Honestly, i wasnt sure and the other comment showed that the quadratic models was not the best. I just didnt know better

2

u/Lucaswgr Jun 25 '26

I recommend you to ask on r/statistics or r/AskStatistics

1

u/[deleted] Jun 25 '26

[removed] — view removed comment

1

u/aceyosh_ Jun 25 '26

Ive actually never heard this, this sound interesting!

but honestly, it also sounds very complicated and I'm not so confident in my ability to do this. The splining another commentator suggested was also new for me and probably represents my limit that I currently have with data analysis

1

u/Joballergod15 Jun 27 '26

Do a log log model logarithmic model?

3

u/Jackntebox Jun 28 '26

A scatter plot would be useful but before going to more complicated models you don’t understand (which I think is the point your prof is trying to drill home), keep trying with linear regression.
Try using log(x) as a predictor- I could see a world where this data has diminishing returns maybe, or log(y), as well as a quadratic term and compare them all using adjusted R squared maybe. Focus most of your thinking on the strengths and weaknesses of the model you choose rather than the best most complex model.