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
16
votes
3 answers

Showing equation of nls model with ggpmisc

R package ggpmisc can be used to show equation of lm model and poly model on ggplot2 (See here for reference). I wonder how I could show nls model equation results on ggplot2 using ggpmisc. Below is my MWE. library(ggpmisc) args <- list(formula = y…
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
16
votes
4 answers

How can I plot the residuals of lm() with ggplot?

I would like to have a nice plot about residuals I got from an lm() model. Currently I use plot(model$residuals), but I want to have something nicer. If I try to plot it with ggplot, I get the error message: ggplot2 doesn't know how to deal with…
Lanza
  • 587
  • 1
  • 5
  • 22
16
votes
6 answers

R error which says "Models were not all fitted to the same size of dataset"

I have created two generalised linear models as follows: glm1 <-glm(Y ~ X1 + X2 + X3, family=binomial(link=logit)) glm2 <-glm(Y ~ X1 + X2, family=binomial(link=logit)) I then use the anova function: anova(glm2,glm1) but get an error…
REnthusiast
  • 1,591
  • 3
  • 16
  • 18
15
votes
4 answers

How to do a Tukey HSD test with the Anova command (car package)

I'm dealing with an unbalanced design/sample and originally learned aov(). I know now that for my ANOVA tests I need to use the Type III Sum of Squares which involves using fitting using lm() rather than using aov(). The problem is getting post-hoc…
leighadlr
  • 159
  • 1
  • 1
  • 5
15
votes
2 answers

Error in dataframe *tmp* replacement has x data has y

I'm a beginner in R. Here is a very simple code where I'm trying to save the residual term: # Create variables for child's EA: dat$cldeacdi <- rowMeans(dat[,c('cdcresp', 'cdcinv')],na.rm=T) dat$cldeacu <- rowMeans(dat[,c('cucresp',…
Marishka Usacheva
  • 347
  • 1
  • 3
  • 13
15
votes
2 answers

Error in lm.fit(x,y,offset = offset, singular.ok,...) 0 non-NA cases with boxcox formula

I am trying to run a boxcox transformation with the following code: urban1 <- subset(ski,urban <= 4,na.rm=TRUE) ski$gender <- as.numeric((as.character(ski$gender)),na.rm=TRUE) urban1 <- as.numeric((as.character(urban1))) x <- (ski$gender*urban1) y…
Vickie Ip
  • 183
  • 1
  • 1
  • 5
14
votes
3 answers

How to use formula in R to exclude main effect but retain interaction

I do not want main effect because it is collinear with a finer factor fixed effect, so it is annoying to have these NA. In this example: lm(y ~ x * z) I want the interaction of x (numeric) and z (factor), but not the main effect of z.
wolfsatthedoor
  • 7,163
  • 18
  • 46
  • 90
14
votes
4 answers

LASSO with $\lambda = 0$ and OLS produce different results in R glmnet

I expect LASSO with no penalization ($\lambda=0$) to yield the same (or very similar) coefficient estimates as an OLS fit. However, I get different coefficient estimates in R putting the same data (x,y) into glmnet(x, y , alpha=1, lambda=0) for…
Helpa
14
votes
3 answers

Fitting a function in R

I have a few datapoints (x and y) that seem to have a logarithmic relationship. > mydata x y 1 0 123 2 2 116 3 4 113 4 15 100 5 48 87 6 75 84 7 122 77 > qplot(x, y, data=mydata, geom="line") Now I would like to find an…
jnns
  • 5,148
  • 4
  • 47
  • 74
14
votes
1 answer

Set one or more of coefficients to a specific integer

I am using a standard lm model and would like to set the coefficients of one or more of my variables to a specific integer. For example, I would like the coefficient of my weather and price variables to be 647 and 15 respectively. I am using the lm…
JackGallas
  • 143
  • 1
  • 4
13
votes
1 answer

Fast pairwise simple linear regression between variables in a data frame

I have seen pairwise or general paired simple linear regression many times on Stack Overflow. Here is a toy dataset for this kind of problem. set.seed(0) X <- matrix(runif(100), 100, 5, dimnames = list(1:100, LETTERS[1:5])) b <- c(1, 0.7, 1.3, 2.9,…
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
13
votes
2 answers

Cluster-Robust Standard Errors in Stargazer

Does anyone know how to get stargazer to display clustered SEs for lm models? (And the corresponding F-test?) If possible, I'd like to follow an approach similar to computing heteroskedasticity-robust SEs with sandwich and popping them into…
RSS
  • 163
  • 1
  • 2
  • 11
13
votes
1 answer

How `poly()` generates orthogonal polynomials? How to understand the "coefs" returned?

My understanding of orthogonal polynomials is that they take the form y(x) = a1 + a2(x - c1) + a3(x - c2)(x - c3) + a4(x - c4)(x - c5)(x - c6)... up to the number of terms desired where a1, a2 etc are coefficients to each orthogonal term (vary…
pyg
  • 716
  • 6
  • 18
13
votes
2 answers

Why the built-in lm function is so slow in R?

I always thought that the lm function was extremely fast in R, but as this example would suggest, the closed solution computed using the solve function is way faster. data<-data.frame(y=rnorm(1000),x1=rnorm(1000),x2=rnorm(1000)) X =…
adaien
  • 1,932
  • 1
  • 12
  • 26
13
votes
3 answers

Linear model (lm) when dependent variable is a factor/categorical variable?

I want to do linear regression with the lm function. My dependent variable is a factor called AccountStatus: 1:0 days in arrears, 2:30-60 days in arrears, 3:60-90 days in arrears and 4:90+ days in arrears. (4) As independent variable I have several…
Tim_Utrecht
  • 1,459
  • 6
  • 24
  • 44