I am confusing regarding the differences between poly and contr.poly in regression.
Both should generate the orthonormal polynomial transformation of a vector. But poly is influenced by the length of the vector while contr.poly only by the number of unique elements in the vector, giving therefore different results.
This is shown passing a vector of 1:10 to the functions:
poly(1:10, 1) %>% unique() %>% sort()
[1] -0.49543369 -0.38533732 -0.27524094 -0.16514456 -0.05504819 0.05504819 0.16514456 0.27524094 0.38533732 0.49543369
attr(C(as.ordered(1:10), ,1), 'contrasts') %>% as.vector()
[1] -0.49543369 -0.38533732 -0.27524094 -0.16514456 -0.05504819 0.05504819 0.16514456 0.27524094 0.38533732 0.49543369
Same result. But if I join the vector twice with c(1:10, 1:10)
I get different results:
poly(c(1:10, 1:10), 1) %>% unique() %>% sort()
[1] -0.35032452 -0.27247463 -0.19462474 -0.11677484 -0.03892495 0.03892495 0.11677484 0.19462474 0.27247463 0.35032452
attr(C(as.ordered(c(1:10,1:10)), ,1), 'contrasts') %>% as.vector()
[1] -0.49543369 -0.38533732 -0.27524094 -0.16514456 -0.05504819 0.05504819 0.16514456 0.27524094 0.38533732 0.49543369
I guess it's because being the vector a factor in the second example the elements are considered only once.
Therefore, which is the difference in interpretation of the two in a regression setting? Which one should be used and in general how one should interpret the coefficients after an orthonormal transformation, what does a change of 1 mean?
Thanks
poly(1:d, degree=d-1)
andcontr.poly(d)
. Start withd<-3
and try it for several more (larger integral) values ofd
. $\endgroup$poly
at stats.stackexchange.com/questions/31858. As you have learned through your experimentation,contr.poly
implements a special case of orthogonal polynomials. $\endgroup$