# How can I compare the slopes of two polynomial regressions from different dataset in R?

I measured air temperature in each hour of a day bellow the canopy of 10 trees and in 10 open areas close to the trees. Therefore, I have two regressions between hours of a day (predictor) and temperature (response). These relationships are not linear and resemble curves in the bell form. Now, I want to compare the slopes of both regressions. Can I use an ANCOVA to evaluate polynomial regressions and select the best model (p<0.05 and higher R²)? Like:

ancova_null_model = lm(response~1*category,                  data)
ancova1           = lm(response~predictor*category,          data)
ancova2           = lm(response~poly(predictor, 2)*category, data)
ancova3           = lm(response~poly(predictor, 3)*category, data)
anova(ancova_null_model, ancova1, ancova2, ancova3)

summary(ancova1)$r.squared summary(ancova2)$r.squared
summary(ancova3)\$r.squared


Or should I conduct two regressions and compare both slopes in another analysis? In this case, how can I compare both slopes of polynomial regressions in an analysis?

The models you describe make sense. Each will test the effect of time of day (including higher order versions in the later models), the effect of location (either under a tree or in the open), and the difference in the effect of time of day between the two locations. That last effect is represented in the interaction(s) between time of day and location.

Note that the title of your post (How can I compare the slopes of two polynomial regressions from different dataset in R?) asks a different question from that tested by the models you provide. By testing those three models, you'll be determining whether the increase in model fit is good relative to the increase in model complexity by adding more terms (you add an additional 2 terms for each increase in polynomial degree, since you're also calculating the interaction). That's one very common way to determine what order polynomial you want to use. It does not, however, tell you whether the two locations differ in their estimates for the effect of time of day ("slopes"). As I mentioned, to answer that question you'll want to look specifically at the interaction between time of day and location for whichever model you want to use.

Since the interaction will be represented in two or three terms for the quadratic and cubic models, respectively, you'll want to test it using anova to compare a model without the interaction to one with it. For example, for the model with linear and quadratic effects of time:

m1 = lm(response~poly(predictor, 2) + category, data) # no interaction
m2 = lm(response~poly(predictor, 2) * category, data) # includes interaction
anova(m1, m2)


A note on poly: By default, poly makes polynomial contrast codes, not polynomial terms for a continuous predictor. If time of day is continuous in your data, you may want to use raw=TRUE in your poly commands.

• Thank you, Rose! Your comment was very helpful. I'll test the interaction. Best wishes. May 2, 2017 at 18:44