Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with poly #214

Open
TylerGrantSmith opened this issue Feb 2, 2022 · 1 comment
Open

Problems with poly #214

TylerGrantSmith opened this issue Feb 2, 2022 · 1 comment

Comments

@TylerGrantSmith
Copy link

I have recently had the need to extract poly data from some models and I was hoping {equatiomatic} had a solution, but I think I need to point out some inaccuracies.

First, it appears the translation fails when poly has max-degree 1, illustrated below (malformed TeX with caret and no power).

Second, poly by default uses orthogonal polynomials, this means that the coefficients given in the associated poly object of the model are not the coefficients of the powers of the unadjusted covariate. This is illustrated below where mod1 and mod3 (using raw polynomials) reduce identical output, as they should, but mod2 equation is incorrect as its coefficients apply to the transformed disp. This can be seen with mod4 where I've explicitly performed the orthogonalization process.

library(equatiomatic)

# no poly
mod1 <- lm(mpg ~ disp, mtcars)

# orthogonal poly
mod2 <- lm(mpg ~ poly(disp, 1), mtcars)

# non-orthogonal poly
mod3 <- lm(mpg ~ poly(disp, 1, raw = T), mtcars)

# `mod2`: equation is incorrect because the coefficients apply to transformed inputs
# `mod2` and `mod3`: poly handling seems to break down when the input is degree 1.
cat(extract_eq(mod1, use_coefs = T))
#> \operatorname{\widehat{mpg}} = 29.6 - 0.04(\operatorname{disp})
cat(extract_eq(mod2, use_coefs = T))
#> \operatorname{\widehat{mpg}} = 20.09 - 28.44(\operatorname{disp^})
cat(extract_eq(mod3, use_coefs = T))
#> \operatorname{\widehat{mpg}} = 29.6 - 0.04(\operatorname{disp,\ 1^})


# Explicitly perform the transformations that `poly` does when using orthogonal polynomials
mtcars2 <- mtcars
mtcars2$disp <- scale(mtcars2$disp)
mtcars2$disp <- mtcars2$disp / sqrt(sum(mtcars2$disp^2))

mod4 <- lm(mpg ~ disp, mtcars2)
mod5 <- lm(mpg ~ poly(disp, 1, raw = T), mtcars2)

cat(extract_eq(mod2, use_coefs = T))
#> \operatorname{\widehat{mpg}} = 20.09 - 28.44(\operatorname{disp^})
cat(extract_eq(mod4, use_coefs = T))
#> \operatorname{\widehat{mpg}} = 20.09 - 28.44(\operatorname{disp})
cat(extract_eq(mod5, use_coefs = T))
#> \operatorname{\widehat{mpg}} = 20.09 - 28.44(\operatorname{disp,\ 1^})

Relevant SO discussions:

@datalorax
Copy link
Owner

Thanks - to be totally honest I never actually tested the branch that included poly() with use_coefs = TRUE so I'm not surprised to see some errors here. I appreciate the issue and I'll try to get it resolved relatively soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants