lm

Pass a vector of variables into lm() formula

只愿长相守 提交于 2019-11-26 04:19:26
问题 I was trying to automate a piece of my code so that programming become less tedious. Basically I was trying to do a stepwise selection of variables using fastbw() in the rms package. I would like to pass the list of variables selected by fastbw() into a formula as y ~ x1+x2+x3 , \"x1\" \"x2\" \"x3\" being the list of variables selected by fastbw() Here is the code I tried and did not work olsOAW0.r060 <- ols(roll_pct~byoy+trans_YoY+change18m, subset= helper==\"POPNOAW0_r060\", na.action = na

How does predict.lm() compute confidence interval and prediction interval?

主宰稳场 提交于 2019-11-26 02:21:52
问题 I ran a regression: CopierDataRegression <- lm(V1~V2, data=CopierData1) and my task was to obtain a 90% confidence interval for the mean response given V2=6 and 90% prediction interval when V2=6 . I used the following code: X6 <- data.frame(V2=6) predict(CopierDataRegression, X6, se.fit=TRUE, interval=\"confidence\", level=0.90) predict(CopierDataRegression, X6, se.fit=TRUE, interval=\"prediction\", level=0.90) and I got (87.3, 91.9) and (74.5, 104.8) which seems to be correct since the PI

Getting Warning: “ &#39;newdata&#39; had 1 row but variables found have 32 rows” on predict.lm

本小妞迷上赌 提交于 2019-11-26 00:48:40
问题 I found peculiarity while using predict and lm function in R. I got different results for data frame and vector for same data. DataFrame code: data(mtcars) fitCar<-lm(mtcars$mpg~mtcars$wt) predict(fitCar, data.frame(x=mean(mtcars$wt)), interval=\"confidence\") Output: fit lwr upr 1 23.282611 21.988668 24.57655 2 21.919770 20.752751 23.08679 3 24.885952 23.383008 26.38890 4 20.102650 19.003004 21.20230 5 18.900144 17.771469 20.02882 6 18.793255 17.659216 19.92729 7 18.205363 17.034274 19.37645

How to succinctly write a formula with many variables from a data frame?

可紊 提交于 2019-11-25 23:52:39
问题 Suppose I have a response variable and a data containing three covariates (as a toy example): y = c(1,4,6) d = data.frame(x1 = c(4,-1,3), x2 = c(3,9,8), x3 = c(4,-4,-2)) I want to fit a linear regression to the data: fit = lm(y ~ d$x1 + d$x2 + d$y2) Is there a way to write the formula, so that I don\'t have to write out each individual covariate? For example, something like fit = lm(y ~ d) (I want each variable in the data frame to be a covariate.) I\'m asking because I actually have 50

Fitting a linear model with multiple LHS

送分小仙女□ 提交于 2019-11-25 23:50:04
问题 I am new to R and I want to improve the following script with an *apply function (I have read about apply , but I couldn\'t manage to use it). I want to use lm function on multiple independent variables (which are columns in a data frame). I used for (i in (1:3) { assign(paste0(\'lm.\',names(data[i])), lm(formula=formula(i),data=data)) } Formula(i) is defined as formula=function(x) { as.formula ( paste(names(data[x]),\'~\', paste0(names(data[-1:-3]), collapse = \'+\')), env=parent.frame() ) }

Getting Warning: “ &#39;newdata&#39; had 1 row but variables found have 32 rows” on predict.lm

泄露秘密 提交于 2019-11-25 22:41:28
I found peculiarity while using predict and lm function in R. I got different results for data frame and vector for same data. DataFrame code: data(mtcars) fitCar<-lm(mtcars$mpg~mtcars$wt) predict(fitCar, data.frame(x=mean(mtcars$wt)), interval="confidence") Output: fit lwr upr 1 23.282611 21.988668 24.57655 2 21.919770 20.752751 23.08679 3 24.885952 23.383008 26.38890 4 20.102650 19.003004 21.20230 5 18.900144 17.771469 20.02882 6 18.793255 17.659216 19.92729 7 18.205363 17.034274 19.37645 8 20.236262 19.136179 21.33635 9 20.450041 19.347720 21.55236 10 18.900144 17.771469 20.02882 11 18

Linear Regression and group by in R

冷暖自知 提交于 2019-11-25 22:32:24
问题 I want to do a linear regression in R using the lm() function. My data is an annual time series with one field for year (22 years) and another for state (50 states). I want to fit a regression for each state so that at the end I have a vector of lm responses. I can imagine doing for loop for each state then doing the regression inside the loop and adding the results of each regression to a vector. That does not seem very R-like, however. In SAS I would do a \'by\' statement and in SQL I would

How to debug “contrasts can be applied only to factors with 2 or more levels” error?

喜夏-厌秋 提交于 2019-11-25 21:57:11
问题 Here are all the variables I\'m working with: str(ad.train) $ Date : Factor w/ 427 levels \"2012-03-24\",\"2012-03-29\",..: 4 7 12 14 19 21 24 29 31 34 ... $ Team : Factor w/ 18 levels \"Adelaide\",\"Brisbane Lions\",..: 1 1 1 1 1 1 1 1 1 1 ... $ Season : int 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 ... $ Round : Factor w/ 28 levels \"EF\",\"GF\",\"PF\",..: 5 16 21 22 23 24 25 26 27 6 ... $ Score : int 137 82 84 96 110 99 122 124 49 111 ... $ Margin : int 69 18 -56 46 19 5 50 69 -26