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

Fit line to data on a log scale in R

Basically, I don't know how to plot a line of best fit on my data once it's a logarithmic scale. I put a linear regression trend line on my plot using lm() and abline(), but now that log="xy" has been added this just produces a horizontal…
Jessica B
  • 321
  • 1
  • 4
  • 15
12
votes
2 answers

What does predict.glm(, type="terms") actually do?

I am confused with the way predict.glm function in R works. According to the help, The "terms" option returns a matrix giving the fitted values of each term in the model formula on the linear predictor scale. Thus, if my model has form f(y) =…
David Dale
  • 10,958
  • 44
  • 73
12
votes
1 answer

Repeat the re-sampling function for 1000 times ? Using lapply?

Please me out! I appreciate any helps ! Thanks! I have trouble on repeat doing re-sampling for 1000 times. I tried using replicate() to do that but it's not working. Is there any other method to do that? Can anyone show me if this maybe done by…
user2978129
  • 191
  • 1
  • 3
  • 10
12
votes
1 answer

Why is using update on a lm inside a grouped data.table losing its model data?

Ok, this is a weird one. I suspect this is a bug inside data.table, but it would be useful if anyone can explain why this is happening - what is update doing exactly? I'm using the list(list()) trick inside data.table to store fitted models. When…
Corvus
  • 7,548
  • 9
  • 42
  • 68
12
votes
3 answers

Add fitted quadratic curve

I'm trying to add a fitted quadratic curve to a plot. abline(lm(data~factor+I(factor^2))) The regression which is displayed is linear and not quadratic and I get this message: Message d'avis : In abline(lm(data ~ factor + I(factor^2)), col = …
Remi.b
  • 17,389
  • 28
  • 87
  • 168
11
votes
3 answers

plot regression line in R

I want to plot a simple regression line in R. I've entered the data, but the regression line doesn't seem to be right. Can someone help? x <- c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120) y <- c(10, 18, 25, 29, 30, 28, 25, 22, 18, 15, 11,…
J.doe
  • 225
  • 1
  • 2
  • 9
11
votes
2 answers

How to interpret lm() coefficient estimates when using bs() function for splines

I'm using a set of points which go from (-5,5) to (0,0) and (5,5) in a "symmetric V-shape". I'm fitting a model with lm() and the bs() function to fit a "V-shape" spline: lm(formula = y ~ bs(x, degree = 1, knots = c(0))) I get the "V-shape" when I…
PDG
  • 287
  • 1
  • 3
  • 14
11
votes
5 answers

Linear Regression and storing results in data frame

I am running a linear regression on some variables in a data frame. I'd like to be able to subset the linear regressions by a categorical variable, run the linear regression for each categorical variable, and then store the t-stats in a data frame.…
Trexion Kameha
  • 3,362
  • 10
  • 34
  • 60
11
votes
1 answer

modify lm or loess function to use it within ggplot2's geom_smooth

I need to modify the lm (or eventually loess) function so I can use it in ggplot2's geom_smooth (or stat_smooth). For example, this is how stat_smooth is used normally: > qplot(data=diamonds, carat, price, facets=~clarity) +…
dalloliogm
  • 8,718
  • 6
  • 45
  • 55
11
votes
1 answer

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) with an obviously correct dataset

I encountered a problem in R when trying to run a simple linear model with a categorical variable as predictor. When running the model, R throws the error `Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : ` The data, however,…
user3588777
  • 113
  • 1
  • 1
  • 5
11
votes
3 answers

Different NA actions for coefficients and summary of linear model in R

In R, when using lm(), if I set na.action = na.pass inside the call to lm(), then in the summary table there is an NA for any coefficient that cannot be estimated (because of missing cells in this case). If, however, I extract just the coefficients…
Jdub
  • 589
  • 5
  • 14
11
votes
5 answers

Why does lm run out of memory while matrix multiplication works fine for coefficients?

I am trying to do fixed effects linear regression with R. My data looks like dte yr id v1 v2 . . . . . . . . . . . . . . . I then decided to simply do this by making yr a factor and use lm: lm(v1 ~…
Alex
  • 19,533
  • 37
  • 126
  • 195
10
votes
4 answers

Function which runs lm over different variables

I would like to create a function which can run a regression model (e.g. using lm) over different variables in a given dataset. In this function, I would specify as arguments the dataset I'm using, the dependent variable y and the independent…
Joost
  • 103
  • 5
10
votes
2 answers

Error in calling `lm` in a `lapply` with `weights` argument

I've encounter a weird behavior when calling lm within a lapply using the weights argument. My code consist of a list of formula on which I run a linear model that I call in lapply. So far it was working: dd <- data.frame(y = rnorm(100), …
Bastien
  • 3,007
  • 20
  • 38
10
votes
2 answers

How to correctly `dput` a fitted linear model (by `lm`) to an ASCII file and recreate it later?

I want to persist a lm object to a file and reload it into another program. I know I can do this by writing/reading a binary file via saveRDS/readRDS, but I'd like to have an ASCII file instead of a binary file. At a more general level, I'd like…
mpettis
  • 3,222
  • 4
  • 28
  • 35