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

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

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

tech