lm

Obtain standard errors of regression coefficients for an “mlm” object returned by `lm()`

倾然丶 夕夏残阳落幕 提交于 2019-11-28 08:28:21
问题 I'd like to run 10 regressions against the same regressor, then pull all the standard errors without using a loop . depVars <- as.matrix(data[,1:10]) # multiple dependent variables regressor <- as.matrix([,11]) # independent variable allModels <- lm(depVars ~ regressor) # multiple, single variable regressions summary(allModels)[1] # Can "view" the standard error for 1st regression, but can't extract... allModels is stored as an "mlm" object, which is really tough to work with. It'd be great

What is the difference between lm(offense$R ~ offense$OBP) and lm(R ~ OBP)?

我怕爱的太早我们不能终老 提交于 2019-11-28 07:42:41
问题 I am trying to use R to create a linear model and use that to predict some values. The subject matter is baseball stats. If I do this: obp <- lm(offense$R ~ offense$OBP) predict(obp, newdata=data.frame(OBP=0.5), interval="predict") I get the error: Warning message: 'newdata' had 1 row but variables found have 20 rows. However, if I do this: attach(offense) obp <- lm(R ~ OBP) predict(obp, newdata=data.frame(OBP=0.5), interval="predict") It works as expected and I get one result. What is the

Looping over combinations of regression model terms

浪尽此生 提交于 2019-11-28 06:13:58
问题 I'm running a regression in the form reg=lm(y ~ x1+x2+x3+z1,data=mydata) In the place of the last term, z1 , I want to loop through a set of different variables, z1 through z10 , running a regression for each with it as the last term. E.g. in second run I want to use reg=lm(y ~ x1+x2+x3+z2,data=mydata) in 3rd run: reg=lm(y ~ x1+x2+x3+z3,data=mydata) How can I automate this by looping through the list of z-variables? 回答1: With this dummy data: dat1 <- data.frame(y = rpois(100,5), x1 = runif

model.matrix(): why do I lose control of contrast in this case

你。 提交于 2019-11-28 05:59:07
问题 Suppose we have a toy data frame: x <- data.frame(x1 = gl(3, 2, labels = letters[1:3]), x2 = gl(3, 2, labels = LETTERS[1:3])) I would like to construct a model matrix # x1b x1c x2B x2C # 1 0 0 0 0 # 2 0 0 0 0 # 3 1 0 1 0 # 4 1 0 1 0 # 5 0 1 0 1 # 6 0 1 0 1 by: model.matrix(~ x1 + x2 - 1, data = x, contrasts.arg = list(x1 = contr.treatment(letters[1:3]), x2 = contr.treatment(LETTERS[1:3]))) but actually I get: # x1a x1b x1c x2B x2C # 1 1 0 0 0 0 # 2 1 0 0 0 0 # 3 0 1 0 1 0 # 4 0 1 0 1 0 # 5 0

Fast linear regression by group

做~自己de王妃 提交于 2019-11-28 05:58:52
I have 500K users and I need to compute a linear regression (with intercept) for each of them. Each user has around 30 records. I tried with dplyr and lm and this is way too slow. Around 2 sec by user. df%>% group_by(user_id, add = FALSE) %>% do(lm = lm(Y ~ x, data = .)) %>% mutate(lm_b0 = summary(lm)$coeff[1], lm_b1 = summary(lm)$coeff[2]) %>% select(user_id, lm_b0, lm_b1) %>% ungroup() ) I tried to use lm.fit which is known to be faster but it doesn't seem to be compatible with dplyr . Is there a fast way to do a linear regression by group? You can just use the basic formulas for calculating

using predict with a list of lm() objects

北慕城南 提交于 2019-11-28 04:24:47
I have data which I regularly run regressions on. Each "chunk" of data gets fit a different regression. Each state, for example, might have a different function that explains the dependent value. This seems like a typical "split-apply-combine" type of problem so I'm using the plyr package. I can easily create a list of lm() objects which works well. However I can't quite wrap my head around how I use those objects later to predict values in a separate data.frame. Here's a totally contrived example illustrating what I'm trying to do: # setting up some fake data set.seed(1) funct <- function

R linear regression issue : lm.fit(x, y, offset = offset, singular.ok = singular.ok, …)

拥有回忆 提交于 2019-11-28 03:56:28
问题 I try a regression with R. I have the following code with no problem in importing the CSV file dat <- read.csv('http://pastebin.com/raw.php?i=EWsLjKNN',sep=";") dat # OK Works fine Regdata <- lm(Y~.,na.action=na.omit, data=dat) summary(Regdata) However when I try a regression it's not working. I get an error message: Erreur dans lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : aucun cas ne contient autre chose que des valeurs manquantes (NA) All my CSV file are numbers and if a

Datatype for linear model in R

痴心易碎 提交于 2019-11-28 02:28:22
I get two vectors as output from R analysis, respectively> [1] "216" "217" "218" "219" "220" "221" "222" "223" "224" "225" "226" [1] 10014733 10014665 10014588 10014504 10014415 10014321 10014227 10014145 10014076 10014014 10013963 Let's call the first one a and the second one b. When I do lm(b~a), it throws out Call: lm(formula = b ~ a) Coefficients: (Intercept) a217 a218 a219 a220 a221 a222 a223 a224 10014733.4 -68.1 -145.8 -229.8 -318.5 -412.8 -506.4 -588.2 -657.4 a225 a226 -719.4 -770.8 I just want to fit a simple line, which can further be plotted with abline... Is this a data structure

Fit many formulae at once, faster options than lapply?

假如想象 提交于 2019-11-28 02:19:19
I have a list for formulas I want to fit to data, rather than running a loop I'd like to do this at once, for performance's sake. The estimations should still be separate, I'm not trying to estimate a SUR or anything. The following code does what I want x <- matrix(rnorm(300),ncol=3) y <- x %*% c(1,2,3)+rnorm(100) formulae <-list(y~x[,1], y~x[,2], y~x[,1] + x[,2]) lapply(formulae,lm) Unfortunately this gets somewhat slow as the length of formulae increases is there a way to truly vectorize this? If it is any help, the only results of lm I care about are coefficients, and some standard errors.

summary dataframe from several multiple regression outputs

旧时模样 提交于 2019-11-28 02:18:51
问题 I am doing multiple OLS regressions. I have used the following lm function: GroupNetReturnsStockPickers <- read.csv("GroupNetReturnsStockPickers.csv", header=TRUE, sep=",", dec=".") ModelGroupNetReturnsStockPickers <- lm(StockPickersNet ~ Mkt.RF+SMB+HML+WML, data=GroupNetReturnsStockPickers) names(GroupNetReturnsStockPickers) summary(ModelGroupNetReturnsStockPickers) Which gives me the summary output of: Call: lm(formula = StockPickersNet ~ Mkt.RF + SMB + HML + WML, data =