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
10
votes
1 answer

linear model with `lm`: how to get prediction variance of sum of predicted values

I'm summing the predicted values from a linear model with multiple predictors, as in the example below, and want to calculate the combined variance, standard error and possibly confidence intervals for this sum. lm.tree <- lm(Volume ~ poly(Girth,2),…
CCID
  • 1,368
  • 5
  • 19
  • 35
10
votes
2 answers

lm() Regression with interactions for an entire dataframe

I know there is a shortcut in Rto run an lm()regression on all a dataframe like this : reg<-lm(y~.,data=df) With df having explanatory variables x1, x2, ... x5, so it is the same as writing reg<-lm(y~x1+x2+x3+x4+x5,data=df) But this doesn't…
etienne
  • 3,648
  • 4
  • 23
  • 37
10
votes
1 answer

lm function in R does not give coefficients for all factor levels in categorical data

I was trying out linear regression with R using categorical attributes and observe that I don't get a coefficient value for each of the different factor levels I have. Please see my code below, I have 5 factor levels for states, but see only 4…
tubby
  • 2,074
  • 3
  • 33
  • 55
10
votes
2 answers

update() a model inside a function with local covariate

I need to update a regression model from inside a function. Ideally, the function should work with any kind of models (lm, glm, multinom, clm). More precisely, I need to add one or several covariates that are defined inside the function. Here is an…
Matthias Studer
  • 1,722
  • 1
  • 10
  • 24
10
votes
3 answers

How to minimize size of object of class "lm" without compromising it being passed to predict()

I want to run lm() on a large dataset with 50M+ observations with 2 predictors. The analysis is run on a remote server with only 10GB for storing the data. I have tested ´lm()´ on 10K observations sampled from the data and the resulting object had…
CptNemo
  • 6,455
  • 16
  • 58
  • 107
10
votes
4 answers

R - interaction with only one factor level in regression

In a regression model is it possible to include an interaction with only one dummy variable of a factor? For example, suppose I have: x: numerical vector of 3 variables (1,2 and 3) y: response variable z: numerical vector Is it possible to build a…
user2081788
  • 103
  • 1
  • 4
10
votes
3 answers

How to predict x values from a linear model (lm)

I have this data set: x <- c(0, 40, 80, 120, 160, 200) y <- c(6.52, 5.10, 4.43, 3.99, 3.75, 3.60) I calculated a linear model using lm(): model <- lm(y ~ x) I want know the predicted values of x if I have new y values, e.g. ynew <- c(5.5, 4.5,…
alexmulo
  • 751
  • 4
  • 11
  • 23
10
votes
2 answers

In R linear model, get p-values for only the interaction coefficients

If I have a summary table for a linear model in R, how can I get the p-values associated with just the interaction estimates, or just the group intercepts, etc., without having to count the row numbers? For example, with a model such as lm(y ~ x +…
Jdub
  • 589
  • 5
  • 14
9
votes
1 answer

Best way in R to pick which level is the base category for a factor in an lm regression

Suppose I want to run a regression using lm and a factor as a right hand side variable. What is the best way to choose which level in the factor is the base category (the one that is excluded to avoid multicollinearity). Note that I am not…
Xu Wang
  • 10,199
  • 6
  • 44
  • 78
9
votes
3 answers

Finding where two linear fits intersect in R

I have two linear fits that I've gotten from lm calls in my R script. For instance... fit1 <- lm(y1 ~ x1) fit2 <- lm(y2 ~ x2) I'd like to find the (x,y) point at which these two lines (fit1 and fit2) intersect, if they intersect at all.
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
9
votes
1 answer

models with 'differences from mean' for all coefficients on categorical variables; get 'contrast coding' to do it?

Suppose we want to do a simple 'descriptive model of income.' Suppose we have three groups, North, Central, and South (think US regions). Comparing otherwise similar groups, suppose average income in the North is 130, Central is 80, and South is…
daaronr
  • 507
  • 1
  • 4
  • 12
9
votes
1 answer

Dropping variable in lm formula still triggers contrast error

I'm trying to run lm() on only a subset of my data, and running into an issue. dt = data.table(y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100), x3 = as.factor(c(rep('men',50), rep('women',50)))) # sample data lm( y ~ ., dt) # Use all x: Works lm(…
Zhaochen He
  • 610
  • 4
  • 12
9
votes
1 answer

Is there a function or package which will simulate predictions for an object returned from lm()?

Is there a single function, similar to "runif", "rnorm" and the like which will produce simulated predictions for a linear model? I can code it on my own, but the code is ugly and I assume that this is something someone has done before. slope =…
PirateGrunt
  • 404
  • 4
  • 11
9
votes
2 answers

How to manually set coefficients for variables in linear model?

In R, how can I set weights for particular variables and not observations in lm() function? Context is as follows. I'm trying to build personal ranking system for particular products, say, for phones. I can build linear model based on price as…
ffriend
  • 27,562
  • 13
  • 91
  • 132
9
votes
2 answers

How to remove a lower order parameter in a model when the higher order parameters remain?

The problem: I cannot remove a lower order parameter (e.g., a main effects parameter) in a model as long as the higher order parameters (i.e., interactions) remain in the model. Even when doing so, the model is refactored and the new model is not…
Henrik
  • 14,202
  • 10
  • 68
  • 91