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
35
votes
3 answers

Plot polynomial regression curve in R

I have a simple polynomial regression which I do as follows attach(mtcars) fit <- lm(mpg ~ hp + I(hp^2)) Now, I plot as follows > plot(mpg~hp) > points(hp, fitted(fit), col='red', pch=20) This gives me the following I want to connect these…
psteelk
  • 1,305
  • 3
  • 16
  • 24
33
votes
5 answers

Extract standard errors from lm object

We got a lm object from and want to extract the standard error lm_aaa <- lm(aaa ~ x + y + z) I know the function summary, names and coefficients. However, summary seems to be the only way to manually access the standard error. Have you any idea how…
Fabian Stolz
  • 1,935
  • 7
  • 27
  • 30
32
votes
2 answers

How does predict.lm() compute confidence interval and prediction interval?

I ran a regression: CopierDataRegression <- lm(V1~V2, data=CopierData1) and my task was to obtain a 90% confidence interval for the mean response given V2=6 and 90% prediction interval when V2=6. I used the following code: X6 <-…
Mitty
  • 475
  • 1
  • 5
  • 9
30
votes
4 answers

Is there a faster lm function

I would like to get the slope of a linear regression fit for 1M separate data sets (1M * 50 rows for data.frame, or 1M * 50 for array). Now I am using the lm() function, which takes a very long time (about 10 min). Is there any faster function for…
Bangyou
  • 9,462
  • 16
  • 62
  • 94
28
votes
11 answers

Linear model function lm() error: NA/NaN/Inf in foreign function call (arg 1)

Say I have data.frame a I use m.fit <- lm(col2 ~ col3 * col4, na.action = na.exclude) col2 has some NA values, col3 and col4 have values less than 1. I keep getting Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :…
Pk.yd
  • 311
  • 1
  • 3
  • 6
26
votes
4 answers

Adding lagged variables to an lm model?

I'm using lm on a time series, which works quite well actually, and it's super super fast. Let's say my model is: > formula <- y ~ x I train this on a training set: > train <- data.frame( x = seq(1,3), y = c(2,1,4) ) > model <- lm( formula, train…
Hugh Perkins
  • 7,975
  • 7
  • 63
  • 71
24
votes
5 answers

How to obtain RMSE out of lm result?

I know there is a small difference between $sigma and the concept of root mean squared error. So, i am wondering what is the easiest way to obtain RMSE out of lm function in R? res<-lm(randomData$price ~randomData$carat+ …
Jeff
  • 7,767
  • 28
  • 85
  • 138
22
votes
1 answer

What does "/" mean in R when writing a regression formula in lm()

The formula is just like this. I don't quite understand the usage of the notion "/". It seems that "/" usually be used in dummy variables. But I am not sure about its usage. lm(y~x/z,data = data.frame(x = rnorm(6), y = rnorm(6), z =…
Roy
  • 539
  • 1
  • 4
  • 12
20
votes
5 answers

Plot fitted line within certain range R

Using R, I would like to plot a linear relationship between two variables, but I would like the fitted line to be present only within the range of the data. For example, if I have the following code, I would like the line to exist only from x and y…
Thraupidae
  • 665
  • 2
  • 5
  • 14
19
votes
2 answers

Using column numbers not names in lm()

Instead of something like lm(bp~height+age, data=mydata) I would like to specify the columns by number, not name. I tried lm(mydata[[1]]~mydata[[2]]+mydata[[3]]) but the problem with this is that, in the fitted model, the coefficients are named…
LeelaSella
  • 757
  • 3
  • 13
  • 24
18
votes
6 answers

using predict with a list of lm() objects

I have data which I regularly run regressions on. Each "chunk" of data gets fit a different regression. Each state, for example, might have a different function that explains the dependent value. This seems like a typical "split-apply-combine" type…
JD Long
  • 59,675
  • 58
  • 202
  • 294
18
votes
5 answers

Fast linear regression by group

I have 500K users and I need to compute a linear regression (with intercept) for each of them. Each user has around 30 records. I tried with dplyr and lm and this is way too slow. Around 2 sec by user. df%>% …
user3904098
17
votes
1 answer

plot.lm(): extracting numbers labelled in the diagnostic Q-Q plot

For the simple example below, you can see that there are certain points that are identified in the ensuing plots. How can I extract the row numbers identified in these plots, especially the Normal Q-Q plot? set.seed(2016) maya <-…
Reuben Mathew
  • 598
  • 4
  • 22
17
votes
1 answer

Running a stepwise linear model with BIC criterion

Is it possible to set a stepwise linear model to use the BIC criteria rather than AIC? I've been trying this but it still calculates each step using AIC values rather than BIC null = lm(data[,1] ~ 1) full = lm(data[,1] ~ age + bmi + gender +…
user2846211
  • 949
  • 6
  • 16
  • 24
16
votes
2 answers

How to update summary when using NeweyWest?

I am using NeweyWest standard errors to correct my lm() / dynlm() output. E.g.: fit1<-dynlm(depvar~covariate1+covariate2) coeftest(fit1,vcov=NeweyWest) Coefficients are displayed the way I´d like to, but unfortunately I loose all the regression…
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207