lm

R: numeric 'envir' arg not of length one in predict()

∥☆過路亽.° 提交于 2019-11-27 02:07:18
问题 I'm trying to predict a value in R using the predict() function, by passing along variables into the model. I am getting the following error: Error in eval(predvars, data, env) : numeric 'envir' arg not of length one Here is my data frame , name df: df <- read.table(text = ' Quarter Coupon Total 1 "Dec 06" 25027.072 132450574 2 "Dec 07" 76386.820 194154767 3 "Dec 08" 79622.147 221571135 4 "Dec 09" 74114.416 205880072 5 "Dec 10" 70993.058 188666980 6 "Jun 06" 12048.162 139137919 7 "Jun 07"

using predict with a list of lm() objects

有些话、适合烂在心里 提交于 2019-11-27 00:23:40
问题 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

Fit many formulae at once, faster options than lapply?

試著忘記壹切 提交于 2019-11-26 23:40:01
问题 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

Shortcut using lm() in R for formula

戏子无情 提交于 2019-11-26 23:36:17
问题 It is possible to use a shortcut for formula in lm() m <- matrix(rnorm(100), ncol=5) lm(m[,1] ~ m[,2:5] here it would be the same as lm(m[,1] ~ m[,2] + m[,3] + m[,4] + m[,5] but in the case when variables are not of the same level (at least this is my assumption for now) this does not work and I get the error: Error in model.frame.default(formula = hm[, 1] ~ hm[, 2:4], drop.unused.levels = TRUE) : invalid type (list) for variable 'hm[, 2:4]' Data (hm): N cor.distance switches time 1 50 0

Plot polynomial regression curve in R

扶醉桌前 提交于 2019-11-26 22:11:26
I have a simple polynomial regression which I do as follows attach(mtcars) fit <- lm(mpg ~ hp + I(hp^2)) Now, I plot as follows > plot(mpg~hp) > points(hp, fitted(fit), col='red', pch=20) This gives me the following I want to connect these points into a smooth curve, using lines gives me the following > lines(hp, fitted(fit), col='red', type='b') What am I missing here. I want the output to be a smooth curve which connects the points Try: lines(sort(hp), fitted(fit)[order(hp)], col='red', type='b') Because your statistical units in the dataset are not ordered, thus, when you use lines it's a

lm(): What is qraux returned by QR decomposition in LINPACK / LAPACK

两盒软妹~` 提交于 2019-11-26 22:09:31
问题 rich.main3 is a linear model in R. I understand the rest of the elements of the list but I don't get what qraux is. The documentation states that it is a vector of length ncol(x) which contains additional information on \bold{Q}". What additional information does it mean? str(rich.main3$qr) qr : num [1:164, 1:147] -12.8062 0.0781 0.0781 0.0781 0.0781 ... ..- attr(*, "dimnames")=List of 2 .. ..$ : chr [1:164] "1" "2" "3" "4" ... .. ..$ : chr [1:147] "(Intercept)" "S2" "S3" "x1" ... ..- attr(*,

How do I extract just the number from a named number (without the name)?

时间秒杀一切 提交于 2019-11-26 22:04:34
I am looking for just the value of the B1(newx) linear model coefficient, not the name. I just want the 0.5 value. I do not want the name "newx". newx <- c(0.5,1.5.2.5) newy <- c(2,3,4) out <- lm(newy ~ newx) out looks like: Call: lm(formula = newy ~ newx) Coefficients: (Intercept) newx 1.5 1.0 I arrived here. But now I am stuck. out$coefficients["newx"] newx 1.0 For a single element like this, use [[ rather than [ . Compare: coefficients(out)["newx"] # newx # 1 coefficients(out)[["newx"]] # [1] 1 More generally, use unname() : unname(coefficients(out)[c("newx", "(Intercept)")]) # [1] 1.0 1.5

predict.lm() with an unknown factor level in test data

99封情书 提交于 2019-11-26 22:03:30
I am fitting a model to factor data and predicting. If the newdata in predict.lm() contains a single factor level that is unknown to the model, all of predict.lm() fails and returns an error. Is there a good way to have predict.lm() return a prediction for those factor levels the model knows and NA for unknown factor levels, instead of only an error? Example code: foo <- data.frame(response=rnorm(3),predictor=as.factor(c("A","B","C"))) model <- lm(response~predictor,foo) foo.new <- data.frame(predictor=as.factor(c("A","B","C","D"))) predict(model,newdata=foo.new) I would like the very last

Export fitted regression splines (constructed by 'bs' or 'ns') as piecewise polynomials

余生长醉 提交于 2019-11-26 21:27:56
问题 Take for instance the following one-knot, degree two, spline: library(splines) library(ISLR) fit.spline <- lm(wage~bs(age, knots=c(42), degree=2), data=Wage) summary(fit.spline) I see estimates that I don't expect. Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 57.349 3.950 14.518 < 2e-16 *** bs(age, knots = c(42), degree = 2)1 59.511 5.786 10.285 < 2e-16 *** bs(age, knots = c(42), degree = 2)2 65.722 4.076 16.122 < 2e-16 *** bs(age, knots = c(42), degree = 2)3 37.170 9.722 3

Linear model function lm() error: NA/NaN/Inf in foreign function call (arg 1)

北城以北 提交于 2019-11-26 21:08:31
问题 Say I have data.frame a I use m.fit <- lm(col2 ~ col3 * col4, na.action = na.exclude) col2 has some NA values, col3 and col4 have values less than 1. I keep getting Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : NA/NaN/Inf in foreign function call (arg 1) I've checked the mailing list and it appears that it is because of the NA s in col2 but I tried using na.action=na.exclude/omit/pass but none of them seem to work. I've tested lm again on first 10 entries,