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
8
votes
2 answers

lm called from inside dlply throws "0 (non-NA) cases" error [r]

I'm using dlply() with a custom function that averages slopes of lm() fits on data that contain some NA values, and I get the error "Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases" This error only…
Drew Steen
  • 16,045
  • 12
  • 62
  • 90
8
votes
1 answer

Prevent NA from being used in a lm regresion

I have a vector Y containing future returns and a vector X contain current returns. The last Y element is NA, as the last current return is also the very end of the available series. X = { 0.1, 0.3, 0.2, 0.5 } Y = { 0.3, 0.2, 0.5, NA } Other = {…
Robert Kubrick
  • 8,413
  • 13
  • 59
  • 91
8
votes
4 answers

Looping linear regression output in a data frame in r

I have a dataset below in which I want to do linear regression for each country and state and then cbind the predicted values in the dataset: Final data frame after adding three more columns: I have done it for one country and one area but want to…
joy_1379
  • 487
  • 3
  • 17
8
votes
6 answers

Extract interaction terms from regression estimates

This is a simple question but I couldn't find a clear and compelling answer anywhere. If I have a regression model with one or more interaction terms, like: mod1 <- lm(mpg ~ factor(cyl) * factor(am), data = mtcars) coef(summary(mod1)) ## …
Thomas
  • 43,637
  • 12
  • 109
  • 140
8
votes
1 answer

Omit output of a factor variable in stargazer?

If I'm running a fixed effects model in r using lm and the factor command, how can I suppress the factor variable coefficients in a stargazer model? i.e. my model is: m1<-lm(GDP~pop_growth + factor(city)) and I want to report findings with only an…
Mackenzie
  • 91
  • 1
  • 3
8
votes
5 answers

Is there a simple command to do leave-one-out cross validation with the lm() function?

Is there a simple command to do leave-one-out cross validation with the lm() function in R? Specifically is there a simple command which for the code below? x <- rnorm(1000,3,2) y <- 2*x + rnorm(1000) pred_error_sq <- c(0) for(i in 1:1000) { x_i…
stollenm
  • 348
  • 1
  • 2
  • 11
8
votes
2 answers

Bizarre behaviour of lm() and predict.lm() depending on use of explicit namespace accessor

I am interested in some disturbing behaviour of the lm function and the associated predict.lm function in R. The splines base package provides the function bs to generate b-spline expansions, which can then be used to fit a spline model using lm, a…
mb7744
  • 390
  • 2
  • 12
8
votes
1 answer

Multivariate regression splines in R

Most people are probably familiar with bs from splines: library(splines) workingModel <- lm(mpg ~ factor(gear) + bs(wt, knots = 5) + hp, data = mtcars) bs(mtcars$wt, knots = 4) This uses a b-spline for the singe variable weight, but you can also…
Carl
  • 5,569
  • 6
  • 39
  • 74
8
votes
1 answer

`lm` summary not display all factor levels

I am running a linear regression on a number of attributes including two categorical attributes, B and F, and I don't get a coefficient value for every factor level I have. B has 9 levels and F has 6 levels. When I initially ran the model (with…
Karen Roberts
  • 83
  • 1
  • 1
  • 4
8
votes
2 answers

How does plot.lm() determine outliers for residual vs fitted plot?

How does plot.lm() determine what points are outliers (that is, what points to label) for residual vs fitted plot? The only thing I found in the documentation is this: Details sub.caption—by default the function call—is shown as a subtitle (under…
3x89g2
  • 257
  • 4
  • 10
8
votes
1 answer

Calculating BIC manually for lm object

I can't quite figure out why I'm having trouble calculating the Bayesian Information Criterion correctly, and was hoping someone could point me in the right direction. I am doing this because I am trying to calculate the BIC manually (for plm…
daanoo
  • 771
  • 5
  • 18
8
votes
2 answers

Use a weights argument in a list of lm lapply calls

Here is my problem (fictional data in order to be reproducible)…
etienne
  • 3,648
  • 4
  • 23
  • 37
8
votes
2 answers

linear regression using lm() - surprised by the result

I used a linear regression on data I have, using the lm function. Everything works (no error message), but I'm somehow surprised by the result: I am under the impression R "misses" a group of points, i.e. the intercept and slope are not the best…
NOTM
  • 147
  • 8
8
votes
2 answers

Stargazer change rownames

I wanted to change the rownames, but the function covariate.labels does not work here like it does using the normal lm function with stargazer. When I try covariate.labels in the below function it changes the column names of the coefficients…
Chevaldamour
  • 89
  • 1
  • 3
8
votes
3 answers

Force certain parameters to have positive coefficients in lm()

I would like to know how to constrain certain parameters in lm() to have positive coefficients. There are a few packages or functions (e.g. display) that can make all coefficients, and the intercept, positive. For instance, in this example, I would…
Yu Deng
  • 1,051
  • 4
  • 18
  • 35