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

Way to extract data from lm-object before function is applied?

let me directly dive into an example to show my problem: rm(list=ls()) n <- 100 df <- data.frame(y=rnorm(n), x1=rnorm(n), x2=rnorm(n) ) fm <- lm(y ~ x1 + poly(x2, 2), data=df) Now, I would like to have a look at the previously used data. This…
coffeinjunky
  • 11,254
  • 39
  • 57
8
votes
2 answers

Use of offset in lm regression - R

I have this code dens <- read.table('DensPiu.csv', header = FALSE) fl <- read.table('FluxPiu.csv', header = FALSE) mydata <- data.frame(c(dens),c(fl)) dat = subset(mydata, dens>=3.15) colnames(dat) <- c("x", "y") attach(dat) and I would like to do…
ac2051
  • 354
  • 1
  • 2
  • 15
8
votes
3 answers

Predict.glm not predicting missing values in response

For some reason, when I specify glms (and lm's too, it turns out), R is not predicting missing values of the data. Here is an example: y = round(runif(50)) y = c(y,rep(NA,50)) x = rnorm(100) m = glm(y~x, family=binomial(link="logit")) p =…
generic_user
  • 3,430
  • 3
  • 32
  • 56
8
votes
3 answers

Extracting PCA axes for further analysis

I am analysing data regarding reed fields. Variables I have measured are water depth, reed height, reed density, etc. As some of the variables are dependent, I performed a PCA in order to reduce these variables to 2 PCA-axes (N=104). For executing…
Koentjes
  • 163
  • 1
  • 3
  • 10
8
votes
1 answer

How does the subset argument work in the lm() function?

I have been trying to figure out how the subset argument in R's lm() function works. Especially the follwoing code seems dubious for me: data(mtcars) summary(lm(mpg ~ wt, data=mtcars)) summary(lm(mpg ~ wt, cyl, data=mtcars)) In every case the…
Seb
  • 5,417
  • 7
  • 31
  • 50
7
votes
2 answers

R calculate robust standard errors (vcovHC) for lm model with singularities

In R, how can I calculate robust standard errors using vcovHC() when some coefficients are dropped due to singularities? The standard lm function seems to do fine calculating normal standard errors for all coefficients that are actually estimated,…
Chris
  • 1,479
  • 2
  • 15
  • 19
7
votes
1 answer

Correcting for robust/clustered standard errors within the lm function or replacing the results

Cross posted on CrossValidated. A while ago, I asked this question, which was about correcting the standard errors when using IV/2SLS and the first stage has a tobit distribution, on which I got an amazing answer from jay.sf (example data at the…
Tom
  • 2,173
  • 1
  • 17
  • 44
7
votes
2 answers

How do I create a "macro" for regressors in R?

For long and repeating models I want to create a "macro" (so called in Stata and there accomplished with global var1 var2 ...) which contains the regressors of the model formula. For example from library(car) lm(income ~ education + prestige, data…
jay.sf
  • 60,139
  • 8
  • 53
  • 110
7
votes
1 answer

geom_smooth custom linear model

While looking at this issue, I couldn't specify a custom linear model to geom_smooth. My code is as follows: example.label <- c("A","A","A","A","A","B","B","B","B","B") example.value <- c(5, 4, 4, 5, 3, 8, 9, 11, 10, 9) example.age <- c(30, 40, 50,…
AK88
  • 2,946
  • 2
  • 12
  • 31
7
votes
1 answer

Does `lm` return `model` for reasons other than `predict`

lm sets model = TRUE by default, meaning the entire dataset used for learning is copied and returned with the fitted object. This is used by predict but creates memory overhead (example below). I am wondering, is the copied dataset used for any…
Simon Jackson
  • 3,134
  • 15
  • 24
7
votes
3 answers

Extract model summaries and store them as a new column

I'm new to the purrr paradigm and am struggling with it. Following a few sources I have managed to get so far as to nest a data frame, run a linear model on the nested data, extract some coefficients from each lm, and generate a summary for each…
niklz
  • 95
  • 7
7
votes
1 answer

Plot conditional density curve `P(Y|X)` along a linear regression line

This is my data frame, with two columns Y (response) and X (covariate): ## Editor edit: use `dat` not `data` dat <- structure(list(Y = c(NA, -1.793, -0.642, 1.189, -0.823, -1.715, 1.623, 0.964, 0.395, -3.736, -0.47, 2.366, 0.634, -0.701,…
Laorie
  • 319
  • 1
  • 7
7
votes
1 answer

Fitting a linear model with multiple LHS

I am new to R and I want to improve the following script with an *apply function (I have read about apply, but I couldn't manage to use it). I want to use lm function on multiple independent variables (which are columns in a data frame). I used for…
bogdan.narcis
  • 151
  • 2
  • 7
7
votes
1 answer

Standardized regression coefficients using lm() and scale() differ from those using lm.beta() or cor()

I have two variables and I want to find the correlation between them. The issues is that I seem to be getting different results depending on which method I use. One method I know of is to run a lm() function with the independent and dependent…
7
votes
2 answers

Drop lines from actual to modeled points in R

Yesterday I worked up an example of the difference between Ordinary Least Squares (OLS) vs. Principal Components Analysis (PCA). For that illustration I wanted to show the errors minimized by OLS and PCA so I plotted the actuals, the predicted line…
JD Long
  • 59,675
  • 58
  • 202
  • 294