How `poly()` generates orthogonal polynomials? How to understand the “coefs” returned?

I have just realized that there was a closely related question Extracting orthogonal polynomial coefficients from R’s poly() function? 2 years ago. The answer there is merely explaining what predict.poly does, but my answer gives a complete picture. Section 1: How does poly represent orthogonal polynomials My understanding of orthogonal polynomials is that they take … Read more

predict.lm() in a loop. warning: prediction from a rank-deficient fit may be misleading

You can inspect the predict function with body(predict.lm). There you will see this line: if (p < ncol(X) && !(missing(newdata) || is.null(newdata))) warning(“prediction from a rank-deficient fit may be misleading”) This warning checks if the rank of your data matrix is at least equal to the number of parameters you want to fit. One way … Read more

lme4::lmer reports “fixed-effect model matrix is rank deficient”, do I need a fix and how to?

You are slightly over-concerned with the warning message: fixed-effect model matrix is rank deficient so dropping 7 columns / coefficients. It is a warning not an error. There is neither misuse of lmer nor ill-specification of model formula, thus you will obtain an estimated model. But to answer your question, I shall strive to explain … Read more

How to Loop/Repeat a Linear Regression in R

You want to run 22,000 linear regressions and extract the coefficients? That’s simple to do from a coding standpoint. set.seed(1) # number of columns in the Lung and Blood data.frames. 22,000 for you? n <- 5 # dummy data obs <- 50 # observations Lung <- data.frame(matrix(rnorm(obs*n), ncol=n)) Blood <- data.frame(matrix(rnorm(obs*n), ncol=n)) Age <- sample(20:80, … Read more