linear-regression
How to overplot a line on a scatter plot in python?
import numpy as np from numpy.polynomial.polynomial import polyfit import matplotlib.pyplot as plt # Sample data x = np.arange(10) y = 5 * x + 10 # Fit with polyfit b, m = polyfit(x, y, 1) plt.plot(x, y, ‘.’) plt.plot(x, b + m * x, ‘-‘) plt.show()
lm function in R does not give coefficients for all factor levels in categorical data
GE is dropped, alphabetically, as the intercept term. As eipi10 stated, you can interpret the coefficients for the other levels in states with GE as the baseline (statesLA = 0.1 meaning LA is, on average, 0.1x more than GE). EDIT: To respond to your updated question: If you include all of the levels in a … Read more
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
R: lm() result differs when using `weights` argument and when using manually reweighted data
Provided you do manual weighting correctly, you won’t see discrepancy. So the correct way to go is: X <- model.matrix(~ q + q2 + b + c, mydata) ## non-weighted model matrix (with intercept) w <- mydata$weighting ## weights rw <- sqrt(w) ## root weights y <- mydata$a ## non-weighted response X_tilde <- rw * … 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
Linear Regression with a known fixed intercept in R
You could subtract the explicit intercept from the regressand and then fit the intercept-free model: > intercept <- 1.0 > fit <- lm(I(x – intercept) ~ 0 + y, lin) > summary(fit) The 0 + suppresses the fitting of the intercept by lm. edit To plot the fit, use > abline(intercept, coef(fit)) P.S. The variables … 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
Linear regression with matplotlib / numpy
arange generates lists (well, numpy arrays); type help(np.arange) for the details. You don’t need to call it on existing lists. >>> x = [1,2,3,4] >>> y = [3,5,7,9] >>> >>> m,b = np.polyfit(x, y, 1) >>> m 2.0000000000000009 >>> b 0.99999999999999833 I should add that I tend to use poly1d here rather than write out … Read more