Questions tagged [lm]

lm is the name of the linear model (i.e. multiple regression) function in the statistics package R. For linear models in general use the `linear-model` tag instead.

lm is the name of the linear model (i.e. multiple regression) function in the statistics package R.

Use this tag to ask about the lm function in R.

For linear models in general use the linear-model tag instead.

Reference: lm help page

350 questions
157
votes
3 answers

How are the standard errors of coefficients calculated in a regression?

For my own understanding, I am interested in manually replicating the calculation of the standard errors of estimated coefficients as, for example, come with the output of the lm() function in R, but haven't been able to pin it down. What is the…
ako
  • 1,673
  • 3
  • 11
  • 7
53
votes
3 answers

Is there any difference between lm and glm for the gaussian family of glm?

Specifically, I want to know if there is a difference between lm(y ~ x1 + x2) and glm(y ~ x1 + x2, family=gaussian). I think that this particular case of glm is equal to lm. Am I wrong?
user3682457
  • 653
  • 1
  • 6
  • 6
44
votes
2 answers

What is the adjusted R-squared formula in lm in R and how should it be interpreted?

What is the exact formula used in R lm() for the Adjusted R-squared? How can I interpret it? Adjusted r-squared formulas There seem to exist several formulas to calculate Adjusted R-squared. Wherry’s formula:…
user1272262
40
votes
2 answers

How to find a good fit for semi-sinusoidal model in R?

I want to assume that the sea surface temperature of the Baltic Sea is the same year after year, and then describe that with a function / linear model. The idea I had was to just input year as a decimal number (or num_months/12) and get out what the…
GaRyu
  • 503
  • 1
  • 5
  • 6
35
votes
5 answers

How to translate the results from lm() to an equation?

We can use lm() to predict a value, but we still need the equation of the result formula in some cases. For example, add the equation to plots.
user27736
  • 359
  • 1
  • 3
  • 4
27
votes
4 answers

Best way to deal with heteroscedasticity?

I have a plot of residual values of a linear model in function of the fitted values where the heteroscedasticity is very clear. However I'm not sure how I should proceed now because as far as I understand this heteroscedasticity makes my linear…
TristanDM
  • 271
  • 1
  • 3
  • 3
25
votes
2 answers

Least Squares Regression Step-By-Step Linear Algebra Computation

As a prequel to a question about linear-mixed models in R, and to share as a reference for beginner/intermediate statistics aficionados, I decided to post as an independent "Q&A-style" the steps involved in the "manual" computation of the…
Antoni Parellada
  • 23,430
  • 15
  • 100
  • 197
18
votes
1 answer

Interpreting output from anova() when using lm() as input

I am learning about building linear regression models by looking over someone elses R code. Here is the example data I am using: v1 v2 v3 response 0.417655013 -0.012026453 -0.528416414 48.55555556 -0.018445979 -0.460809371 …
derNincompoop
  • 693
  • 2
  • 6
  • 14
14
votes
1 answer

R: test normality of residuals of linear model - which residuals to use

I would like to do a Shapiro Wilk's W test and Kolmogorov-Smirnov test on the residuals of a linear model to check for normality. I was just wondering what residuals should be used for this - the raw residuals, the Pearson residuals, studentized…
Tom Wenseleers
  • 2,413
  • 1
  • 21
  • 39
14
votes
1 answer

Why does R's lm() return different coefficient estimates than my textbook?

Background I'm trying to understand the first example in a course on fitting models (so this may seem ludicrously simple). I've done the calculations by hand and they match the example, but when I repeat them in R, the model coefficients are off. I…
post-hoc
  • 677
  • 1
  • 6
  • 14
12
votes
1 answer

Recalculate log-likelihood from a simple R lm model

I'm simply trying to recalculate with dnorm() the log-likelihood provided by the logLik function from a lm model (in R). It works (almost perfectly) for high number of data (eg n=1000) : > n <- 1000 > x <- 1:n > set.seed(1) > y <- 10 + 2*x +…
Gilles
  • 405
  • 1
  • 3
  • 9
11
votes
2 answers

Robust regression inference and Sandwich estimators

Can you give me an example of the use of sandwich estimators in order to perform robust regression inference? I can see the example in ?sandwich, but I don't quite understand how we can go from lm(a ~ b, data) (r-coded) to an estimate and a p value…
Remi.b
  • 4,572
  • 12
  • 34
  • 64
11
votes
1 answer

Repeated measures anova: lm vs lmer

I'm trying to reproduce several interaction test between with both lm and lmer on repeated measures (2x2x2). The reason I want to compare both methods is because SPSS's GLM for repeated measures yields the exact same results as the lm approach…
mat
  • 639
  • 1
  • 4
  • 19
10
votes
3 answers

Why is linear regression not able to predict the outcome of a simple deterministic sequence?

A colleague of mine sent me this problem apparently making the rounds on the internet: If $3 = 18, 4 = 32, 5 = 50, 6 = 72, 7 = 98$, Then, $10 =$ ? The answer seems to be 200. 3*6 4*8 5*10 6*12 7*14 8*16 9*18 10*20=200 When I do a…
Brett Phinney
  • 413
  • 1
  • 4
  • 11
9
votes
1 answer

Difference between linear model and linear regression

I am interested in the difference between a linear regression and a linear model. In my understanding, linear regression is part of a larger family of linear models but both terms are often used as synonyms. Now, it has been suggested to me, that I…
Syrafina
  • 91
  • 1
  • 4
1
2 3
23 24