lm

How to add all variables its second degree in lm()? [duplicate]

帅比萌擦擦* 提交于 2019-12-02 13:51:11
This question already has an answer here: R:fit dynamic number of explanatory variable into polynomial regression 1 answer I have a dataframe with 16 variables. When I do multiple linear regression I do the following: fit <- lm(y ~ .,data=data) Now, I know how to add a second degree term of one of the variables: fit2 <- lm(y ~ poly(x1,2) + .,data=data) But now I don't want to write this out for all of my 16 variables. How can I do this in an easy way for all my variables? When assuming the first variable in data is our 'y', we get this: as.formula( paste('y ~',paste('poly(',colnames(data[-1]),

Incorrect abline line for a regression model with intercept in R

China☆狼群 提交于 2019-12-02 12:11:19
问题 (reproducible example given) In the following, I get an abline line with y-intercept is about 30, but the regression says y-intercept should be 37.2851 Where am I wrong? mtcars$mpg # 21.0 21.0 22.8 ... 21.4 (32 obs) mtcars$wt # 2.620 2.875 2.320 ... 2.780 (32 obs) regression1 <- lm(mtcars$mpg ~ mtcars$wt) coef(regression1) # mpg ~ 37.2851 - 5.3445wt plot(mtcars$mpg ~ mtcars$wt, pch=19, col='gray50') # pch: shape of points abline(h=mean(mtcars$mpg), lwd=2, col ='darkorange') # The y-coordinate

Get p-value for group mean difference without refitting linear model with a new reference level

隐身守侯 提交于 2019-12-02 11:14:23
问题 When we have a linear model with a factor variable X (with levels A , B , and C ) y ~ factor(X) + Var2 + Var3 The result shows the estimate XB and XC which is differences B - A and C - A . (suppose that the reference is A ). If we want to know the p-value of the difference between B and C : C - B , we should designate B or C as a reference group and re-run the model. Can we get the p-values of the effect B - A , C - A , and C - B at one time? 回答1: You are looking for linear hypothesis test by

How can I force dropping intercept or equivalent in this linear model?

允我心安 提交于 2019-12-02 10:47:25
Consider the following table : DB <- data.frame( Y =rnorm(6), X1=c(T, T, F, T, F, F), X2=c(T, F, T, F, T, T) ) Y X1 X2 1 1.8376852 TRUE TRUE 2 -2.1173739 TRUE FALSE 3 1.3054450 FALSE TRUE 4 -0.3476706 TRUE FALSE 5 1.3219099 FALSE TRUE 6 0.6781750 FALSE TRUE I'd like to explain my quantitative variable Y by two binary variables (TRUE or FALSE) without intercept. The argument of this choice is that, in my study, we can't observe X1=FALSE and X2=FALSE at the same time, so it doesn't make sense to have a mean, other than 0, for this level. With intercept m1 <- lm(Y~X1+X2, data=DB) summary(m1)

predict lm function in R (multiple linear regression)

亡梦爱人 提交于 2019-12-02 08:35:48
I did a multiple linear regression in R using the function lm and I want to use it to predict several values. So I'm trying to use the function predict() . Here is my code: new=data.frame(t=c(10, 20, 30)) v=1/t LinReg<-lm(p ~ log(t) + v) Pred=predict(LinReg, new, interval="confidence") So I would like to predict the values of p when t=c(10,20,30...) . However, this is not working and I don't see why. The error message I get is: "Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : variable lengths differ (found for 'vart') In addition: Warning message:

Get p-value for group mean difference without refitting linear model with a new reference level

孤街浪徒 提交于 2019-12-02 05:30:34
When we have a linear model with a factor variable X (with levels A , B , and C ) y ~ factor(X) + Var2 + Var3 The result shows the estimate XB and XC which is differences B - A and C - A . (suppose that the reference is A ). If we want to know the p-value of the difference between B and C : C - B , we should designate B or C as a reference group and re-run the model. Can we get the p-values of the effect B - A , C - A , and C - B at one time? 李哲源 You are looking for linear hypothesis test by check p-value of some linear combination of regression coefficients. Based on my answer: How to conduct

Coefficient table does not have NA rows in rank-deficient fit; how to insert them?

吃可爱长大的小学妹 提交于 2019-12-02 05:16:53
问题 library(lmPerm) x <- lmp(formula = a ~ b * c + d + e, data = df, perm = "Prob") summary(x) # truncated output, I can see `NA` rows here! #Coefficients: (1 not defined because of singularities) # Estimate Iter Pr(Prob) #b 5.874 51 1.000 #c -30.060 281 0.263 #b:c NA NA NA #d1 -31.333 60 0.633 #d2 33.297 165 0.382 #d3 -19.096 51 1.000 #e 1.976 NA NA I want to pull out the Pr(Prob) results for everything, but y <- summary(x)$coef[, "Pr(Prob)"] #(Intercept) b c d1 d2 # 0.09459459 1.00000000 0

Linear regression with product of factor and independent variable

喜夏-厌秋 提交于 2019-12-02 04:36:13
I am try to estimate a demand model: d_t^k = a_t - b^k p_t^k + e_t^k The indices t are for week number, k are for product number. The demand for each product d_t^k depends on the general seasonality that is shared by all the products a_t , and is a affine function of the price of the product in that week p_t^k , plus some normal random error e_t^k . However, if I use the following lm function call, it gives me a single coefficient b for price , when what I want is one coefficient per product b^k for price^k . lm(demand ~ factor(week) + price, data = df) What is the right way to express the

Linear model singular because of large integer datetime in R?

核能气质少年 提交于 2019-12-02 04:22:38
Simple regression of random normal on date fails, but identical data with small integers instead of dates works as expected. # Example dataset with 100 observations at 2 second intervals. set.seed(1) df <- data.frame(x=as.POSIXct("2017-03-14 09:00:00") + seq(0, 199, 2), y=rnorm(100)) #> head(df) # x y # 1 2017-03-14 09:00:00 -0.6264538 # 2 2017-03-14 09:00:02 0.1836433 # 3 2017-03-14 09:00:04 -0.8356286 # Simple regression model. m <- lm(y ~ x, data=df) The slope is missing due to singularities in the data. Calling the summary demonstrates this: summary(m) # Coefficients: (1 not defined

Table of multiple lm() models using apsrtable in Rmarkdown

早过忘川 提交于 2019-12-02 03:51:08
问题 Goal Present the results of multiple models, created using the lm() function, together in a nicely-formatted table. This table will be generated in a .Rmd file and output to a PDF document. Proposed Solution In Reproducible Research with R and RStudio , there is an example using the apsrtable() function to display multiple models side-by-side. This book provides the following code (p. 173-174): Code \begin{table} \caption{Example Nested Estimates Table with \emph{aprstable}} \label