lm

How to write interactions in regressions in R?

心不动则不痛 提交于 2019-12-25 05:00:20
问题 DF <- data.frame(factor1=rep(1:4,1000), factor2 = rep(1:4,each=1000),base = rnorm(4000,0,1),dep=rnorm(4000,400,5)) DF$f1_1 = DF$factor1 == 1 DF$f1_2 = DF$factor1 == 2 DF$f1_3 = DF$factor1 == 3 DF$f1_4 = DF$factor1 == 4 DF$f2_1 = DF$factor2 == 1 DF$f2_2 = DF$factor2 == 2 DF$f2_3 = DF$factor2 == 3 DF$f2_4 = DF$factor2 == 4 I want to run the following regression: Dep = (f1_1 + f1_2 + f1_3 + f1_4)*(f2_1 + f2_2 + f2_3 + f2_4)*(base+base^2+base^3+base^4+base^5) Is there a smarter way to do it? 回答1:

Extract Adj R Square from a list of lm results [duplicate]

∥☆過路亽.° 提交于 2019-12-24 16:56:24
问题 This question already has answers here : Print R-squared for all of the models fit with lmList (2 answers) Closed 3 years ago . I have used 50 predictor variables to create combinations of more than 2 million regression models for a single outcome variable. Most of these are nonsense--I want to eliminate all models with an Adjusted R-Square (AR2) below 0.7, and whose members have a vif>4 (from the car package). I have been first creating a list of all of the models (b), and in a second step,

Linear regression with `lm()`: prediction interval for aggregated predicted values

故事扮演 提交于 2019-12-24 10:47:21
问题 I'm using predict.lm(fit, newdata=newdata, interval="prediction") to get predictions and their prediction intervals (PI) for new observations. Now I would like to aggregate (sum and mean) these predictions and their PI's based on an additional variable (i.e. a spatial aggregation on the zip code level of predictions for single households). I learned from StackExchange, that you cannot aggregate the prediction intervals of single predictions just by aggregating the limits of the prediction

Calling update within a lapply within a function, why isn't it working?

試著忘記壹切 提交于 2019-12-24 09:43:26
问题 This a a follow up question from Error in calling `lm` in a `lapply` with `weights` argument but it may not be the same problem (but still related). Here is a reproducible example: dd <- data.frame(y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100), x3 = rnorm(100), x4 = rnorm(100), wg = runif(100,1,100)) ls.form <- list( formula(y~x1+x2), formula(y~x3+x4), formula(y~x1|x2|x3), formula(y~x1+x2+x3+x4) ) I have a function that takes different arguments (1- a subsample, 2- a colname for the

Using linear regression (lm) in R caret, how do I force the intercept through 0? [duplicate]

空扰寡人 提交于 2019-12-24 08:57:20
问题 This question already has answers here : Fit a no-intercept model in caret (2 answers) Closed 3 months ago . I'm trying to use R caret to perform cross-validation of my linear regression models. In some cases I want to force the intercept through 0. I have tried the following, using the standard lm syntax: regressControl <- trainControl(method="repeatedcv", number = 4, repeats = 5 ) regress <- train(y ~ 0 + x, data = myData, method = "lm", trControl = regressControl) Call: lm(formula =

Easily performing the same regression on different datasets

点点圈 提交于 2019-12-24 04:44:30
问题 I'm performing the same regression on several different datasets (same dependent and independe variables). However, there are many independent variables, and I often want to test adding/removing different variables. I'd like to avoid making all these changes to different lines of code, just because they use different datasets. Can I instead just copy the formula that was used to create some object, and then create a new object using a different dataset? For example, something like: fit1 <- lm

How to predict a new value using simple linear regression log(y)=b0+b1*log(x)

随声附和 提交于 2019-12-24 00:24:22
问题 How to predict a new given value of body using the ml2 model below, and interpret its output (new predicted output only, not model) Using Animals dataset from MASS package to build a simple linear regression model ml2<-lm(log(brain)~log(body),data=Animals) predict a new given body of 468 pred_body<-data.frame(body=c(468)) predict(ml2,new, interval="confidence") fit lwr upr 1 5.604506 4.897498 6.311513 But i am not so sure predicted y(brain) =5.6 or log(brain)=5.6? How could we get the

How to plot confidence bands for my weighted log-log linear regression?

橙三吉。 提交于 2019-12-23 23:18:11
问题 I need to plot an exponential species-area relationship using the exponential form of a weighted log-log linear model, where mean species number per location/Bank ( sb$NoSpec.mean ) is weighted by the variance in species number per year ( sb$NoSpec.var ). I am able to plot the fit, but have issues figuring out how to plot the confidence intervals around this fit. The following is the best I have come up with so far. Any advice for me? # Data df <- read.csv("YearlySpeciesCount_SizeGroups.csv")

Rolling prediction in a data frame using dplyr and rollapply

拈花ヽ惹草 提交于 2019-12-23 18:33:34
问题 My first question here :) My goal is: Given a data frame with predictors (each column a predictor / rows observations) fit a regression using lm and then predict the value using the last observation using a rolling window. The data frame looks like: > DfPredictor[1:40,] Y X1 X2 X3 X4 X5 1 3.2860 192.5115 2.1275 83381 11.4360 8.7440 2 3.2650 190.1462 2.0050 88720 11.4359 8.8971 3 3.2213 192.9773 2.0500 74130 11.4623 8.8380 4 3.1991 193.7058 2.1050 73930 11.3366 8.7536 5 3.2224 193.5407 2.0275

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

北城以北 提交于 2019-12-23 09:37:56
问题 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 . 回答1: Introduction R documentation of ?formula says: The ‘*’ operator denotes factor crossing: ‘a * b’ interpreted as ‘a + b + a : b So it sounds like that dropping main effect is straightforward, by just doing one of the following: a + a:b ## main effect on `b