Questions tagged [lm]

The lm function is used to fit linear models in R. It can be used to carry out regression, single stratum analysis of variance and analysis of covariance.

lm is an R function to fit linear models. It can be used to carry out regression, single stratum analysis of variance and analysis of covariance.

Usage

lm(formula, data, subset, weights, na.action,
   method = "qr", model = TRUE, x = FALSE, y = FALSE, qr = TRUE,
   singular.ok = TRUE, contrasts = NULL, offset, ...)

For further help, see Section 11 (Statistical models in R), especially Section 11.2 Linear Models in the introductory manual: An Introduction to R

1912 questions
158
votes
6 answers

How to succinctly write a formula with many variables from a data frame?

Suppose I have a response variable and a data containing three covariates (as a toy example): y = c(1,4,6) d = data.frame(x1 = c(4,-1,3), x2 = c(3,9,8), x3 = c(4,-4,-2)) I want to fit a linear regression to the data: fit = lm(y ~ d$x1 + d$x2 +…
grautur
  • 29,955
  • 34
  • 93
  • 128
133
votes
10 answers

Linear Regression and group by in R

I want to do a linear regression in R using the lm() function. My data is an annual time series with one field for year (22 years) and another for state (50 states). I want to fit a regression for each state so that at the end I have a vector of lm…
JD Long
  • 59,675
  • 58
  • 202
  • 294
118
votes
4 answers

How do I extract just the number from a named number (without the name)?

I am looking for just the value of the B1(newx) linear model coefficient, not the name. I just want the 0.5 value. I do not want the name "newx". newx <- c(0.5,1.5,2.5) newy <- c(2,3,4) out <- lm(newy ~ newx) out looks like: Call: lm(formula =…
Andre Mikulec
  • 1,302
  • 2
  • 10
  • 8
88
votes
5 answers

Getting Warning: " 'newdata' had 1 row but variables found have 32 rows" on predict.lm

I found peculiarity while using predict and lm function in R. I got different results for data frame and vector for same data. DataFrame code: data(mtcars) fitCar<-lm(mtcars$mpg~mtcars$wt) predict(fitCar, data.frame(x=mean(mtcars$wt)), …
Kush Patel
  • 3,685
  • 5
  • 42
  • 65
86
votes
4 answers

Extract regression coefficient values

I have a regression model for some time series data investigating drug utilisation. The purpose is to fit a spline to a time series and work out 95% CI etc. The model goes as follows: id <- ts(1:length(drug$Date)) a1 <- ts(drug$Rate) a2 <-…
John
  • 41,131
  • 31
  • 82
  • 106
86
votes
3 answers

Get coefficients estimated by maximum likelihood into a stargazer table

Stargazer produces very nice latex tables for lm (and other) objects. Suppose I've fit a model by maximum likelihood. I'd like stargazer to produce a lm-like table for my estimates. How can I do this? Although it's a bit hacky, one way might be…
Adrian
  • 3,138
  • 2
  • 28
  • 39
69
votes
4 answers

Predict() - Maybe I'm not understanding it

I posted earlier today about an error I was getting with using the predict function. I was able to get that corrected, and thought I was on the right path. I have a number of observations (actuals) and I have a few data points that I want to…
mikebmassey
  • 8,354
  • 26
  • 70
  • 95
62
votes
3 answers

How to debug "contrasts can be applied only to factors with 2 or more levels" error?

Here are all the variables I'm working with: str(ad.train) $ Date : Factor w/ 427 levels "2012-03-24","2012-03-29",..: 4 7 12 14 19 21 24 29 31 34 ... $ Team : Factor w/ 18 levels "Adelaide","Brisbane Lions",..: 1 1 1…
Troy
  • 683
  • 1
  • 7
  • 8
62
votes
3 answers

Extract R-square value with R in linear models

I know that using summary will help me to do this manually, however, I will have to calculted tons of R-squared values. Therefore, I need the computer to extract it for me. Here is a simple…
Zander
  • 988
  • 1
  • 9
  • 16
61
votes
1 answer

R: numeric 'envir' arg not of length one in predict()

I'm trying to predict a value in R using the predict() function, by passing along variables into the model. I am getting the following error: Error in eval(predvars, data, env) : numeric 'envir' arg not of length one Here is my data frame,…
mikebmassey
  • 8,354
  • 26
  • 70
  • 95
59
votes
6 answers

Pass a vector of variables into lm() formula

I was trying to automate a piece of my code so that programming become less tedious. Basically I was trying to do a stepwise selection of variables using fastbw() in the rms package. I would like to pass the list of variables selected by fastbw()…
Anand
  • 759
  • 1
  • 6
  • 10
56
votes
3 answers

extracting standardized coefficients from lm in R

My apologies for the dumb question...but I can't seem to find a simple solution I want to extract the standardized coefficients from a fitted linear model (in R) there must be a simple way or function that does that. can you tell me what is it? EDIT…
amit
  • 3,332
  • 6
  • 24
  • 32
47
votes
3 answers

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

This R code throws a warning # Fit regression model to each cluster y <- list() length(y) <- k vars <- list() length(vars) <- k f <- list() length(f) <- k for (i in 1:k) { vars[[i]] <- names(corc[[i]][corc[[i]]!= "1"]) f[[i]] <-…
Mahsa
  • 531
  • 1
  • 5
  • 9
45
votes
3 answers

Linear Regression with a known fixed intercept in R

I want to calculate a linear regression using the lm() function in R. Additionally I want to get the slope of a regression, where I explicitly give the intercept to lm(). I found an example on the internet and I tried to read the R-help "?lm"…
R_User
  • 10,682
  • 25
  • 79
  • 120
39
votes
7 answers

predict.lm() with an unknown factor level in test data

I am fitting a model to factor data and predicting. If the newdata in predict.lm() contains a single factor level that is unknown to the model, all of predict.lm() fails and returns an error. Is there a good way to have predict.lm() return a…
Stephan Kolassa
  • 7,953
  • 2
  • 28
  • 48
1
2 3
99 100