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

Call lm with a model matrix instead of a formula

I want to fit a linear model in R using lm to obtain coefficient estimates and p-values + p-value for total model fit (ANOVA-like), so basically the output from summary.lm. The problem is that I want to use my own model matrix, instead of specifying…
Misconstruction
  • 1,839
  • 4
  • 17
  • 23
7
votes
4 answers

Test if the slope in simple linear regression equals to a given constant in R

I want to test if the slope in a simple linear regression is equal to a given constant other than zero. > x <- c(1,2,3,4) > y <- c(2,5,8,13) > fit <- lm(y ~ x) > summary(fit) Call: lm(formula = y ~ x) Residuals: 1 2 3 4 0.4 -0.2…
JACKY88
  • 3,391
  • 5
  • 32
  • 48
7
votes
1 answer

Format of R's lm() Formula with a Transformation

I can't quite figure out how to do the following in one line: data(attenu) x_temp = attenu$accel^(1/4) y_temp = log(attenu$dist) best_line = lm(y_temp ~ x_temp) Since the above works, I thought I could do the following: data(attenu) best_line = lm(…
nfmcclure
  • 3,011
  • 3
  • 24
  • 40
7
votes
1 answer

applying lm to multiple datasets

Below are 4 datasets (I've just created them randomly for the sake of providing a reproducible code). I created a list of these so I could apply "lm" to these multiple datasets at once : H<-data.frame(replicate(10,sample(0:20,10,rep=TRUE))) …
oivemaria
  • 453
  • 4
  • 20
7
votes
3 answers

Recursive regression in R

Say I have a data frame in R as follows: > set.seed(1) > X <- runif(50, 0, 1) > Y <- runif(50, 0, 1) > df <- data.frame(X,Y) > head(df) X Y 1 0.2655087 0.47761962 2 0.3721239 0.86120948 3 0.5728534 0.43809711 4 0.9082078…
Grant
  • 1,536
  • 13
  • 25
7
votes
3 answers

Passing Argument to lm in R within Function

I would like to able to call lm within a function and specify the weights variable as an argument passed to the outside function that is then passed to lm. Below is a reproducible example where the call works if it is made to lm outside of a…
Michael
  • 13,244
  • 23
  • 67
  • 115
7
votes
3 answers

How to perform lm.ridge summary?

I wonder is there a way to output summary for ridge regression in R? It is a result of lm.ridge{MASS} function. For standard linear model you just do summary(lm_model) but what about ridge regression model? Thanks for help.
Marcin
  • 7,834
  • 8
  • 52
  • 99
7
votes
1 answer

Use Predict on data.table with Linear Regression

Regrad to this Post, I have created an example to play with linear regression on data.table package as follows: ## rm(list=ls()) # anti-social library(data.table) set.seed(1011) DT = data.table(group=c("b","b","b","a","a","a"), …
newbie
  • 917
  • 8
  • 21
7
votes
1 answer

How to use division in lm

I would like to use lm to predict y by a/b (the quotient of a by b) when I do lm(y~a/b) I get a and b interactions, how can I do ?
statquant
  • 13,672
  • 21
  • 91
  • 162
7
votes
4 answers

warning when calculating predicted values

working with a data frame x Date Val 1/1/2012 7 2/1/2012 9 3/1/2012 20 4/1/2012 24 5/1/2012 50 a <- seq(as.Date(tail(x, 1)$Date), by="month", length=5) a <- data.frame(a) x.lm <- lm(x$Val ~…
user1471980
  • 10,127
  • 48
  • 136
  • 235
6
votes
1 answer

lm predict won't predict

I have 2 data frames. One is training data (pubs1), the other (pubs2) test data. I can create a linear regression object but am unable to create a prediction. This is not my first time doing this and can't figure out what is going wrong. >…
screechOwl
  • 27,310
  • 61
  • 158
  • 267
6
votes
3 answers

Why does lm return values when there is no variance in the predicted value?

Consider the following R code (which, I think, eventually calls some Fortran): X <- 1:1000 Y <- rep(1,1000) summary(lm(Y~X)) Why are values returned by summary? Shouldn't this model fail to fit since there is no variance in Y? More importantly,…
russellpierce
  • 4,583
  • 2
  • 32
  • 44
6
votes
1 answer

R: cannot predict specific value

> age <- c(23,19,25,10,9,12,11,8) > steroid <- c(27.1,22.1,21.9,10.7,7.4,18.8,14.7,5.7) > sample <- data.frame(age,steroid) > fit2 <- lm(sample$steroid~poly(sample$age,2,raw=TRUE)) > fit2 Call: lm(formula = sample$steroid ~ poly(sample$age, 2, raw…
sujinge9
  • 231
  • 3
  • 4
  • 7
6
votes
2 answers

Understanding lm and environment

I'm executing lm() with arguments formula, data, na.action, and weights. My weights are stored in a numeric variable. When I specify formula as a character (i.e. formula = "Response~0+."), I get an error that weights is not of the proper length…
Suraj
  • 35,905
  • 47
  • 139
  • 250
6
votes
2 answers

select non-missing variables in a purrr loop

Consider this example mydata <- data_frame(ind_1 = c(NA,NA,3,4), ind_2 = c(2,3,4,5), ind_3 = c(5,6,NA,NA), y = c(28,34,25,12), group = c('a','a','b','b')) >…
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235