lm
predict.lm() with an unknown factor level in test data
You have to remove the extra levels before any calculation, like: > id <- which(!(foo.new$predictor %in% levels(foo$predictor))) > foo.new$predictor[id] <- NA > predict(model,newdata=foo.new) 1 2 3 4 -0.1676941 -0.6454521 0.4524391 NA This is a more general way of doing it, it will set all levels that do not occur in the original data to NA. … 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 do I extract just the number from a named number (without the name)?
For a single element like this, use [[ rather than [. Compare: coefficients(out)[“newx”] # newx # 1 coefficients(out)[[“newx”]] # [1] 1 More generally, use unname(): unname(coefficients(out)[c(“newx”, “(Intercept)”)]) # [1] 1.0 1.5 head(unname(mtcars)) # NA NA NA NA NA NA NA NA NA NA NA # Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 … Read more
How do I create a “macro” for regressors in R?
Here are some alternatives. No packages are used in the first 3. 1) reformulate fo <- reformulate(regressors, response = “income”) lm(fo, Duncan) or you may wish to write the last line as this so that the formula that is shown in the output looks nicer: do.call(“lm”, list(fo, quote(Duncan))) in which case the Call: line of … Read more
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
Plot polynomial regression curve in R
I like to use ggplot2 for this because it’s usually very intuitive to add layers of data. library(ggplot2) fit <- lm(mpg ~ hp + I(hp^2), data = mtcars) prd <- data.frame(hp = seq(from = range(mtcars$hp)[1], to = range(mtcars$hp)[2], length.out = 100)) err <- predict(fit, newdata = prd, se.fit = TRUE) prd$lci <- err$fit – 1.96 … Read more