lm

linear regression using lm() - surprised by the result

为君一笑 提交于 2019-12-09 03:11:14
问题 I used a linear regression on data I have, using the lm function. Everything works (no error message), but I'm somehow surprised by the result: I am under the impression R "misses" a group of points, i.e. the intercept and slope are not the best fit. For instance, I am referring to the group of points at coordinates x=15-25,y=0-20. My questions: is there a function to compare fit with "expected" coefficients and "lm-calculated" coefficients? have I made a silly mistake when coding, leading

How to correctly `dput` a fitted linear model (by `lm`) to an ASCII file and recreate it later?

梦想与她 提交于 2019-12-08 22:29:39
问题 I want to persist a lm object to a file and reload it into another program. I know I can do this by writing/reading a binary file via saveRDS / readRDS , but I'd like to have an ASCII file instead of a binary file. At a more general level, I'd like to know why my idioms for reading in dput output in general is not behaving as I'd expect. Below are examples of making a simple fit, and successful and unsuccessful recreations of the model: dat_train <- data.frame(x=1:4, z=c(1, 2.1, 2.9, 4)) fit

Prevent NA from being used in a lm regresion

廉价感情. 提交于 2019-12-08 19:37:41
问题 I have a vector Y containing future returns and a vector X contain current returns. The last Y element is NA, as the last current return is also the very end of the available series. X = { 0.1, 0.3, 0.2, 0.5 } Y = { 0.3, 0.2, 0.5, NA } Other = { 5500, 222, 523, 3677 } lm(Y ~ X + Other) I want to make sure that the last element of each series is not included in the regression. I read the na.action documentation but I'm not clear if this is the default behaviour. For cor(), is this the correct

R calculate robust standard errors (vcovHC) for lm model with singularities

爷,独闯天下 提交于 2019-12-08 17:30:39
问题 In R, how can I calculate robust standard errors using vcovHC() when some coefficients are dropped due to singularities? The standard lm function seems to do fine calculating normal standard errors for all coefficients that are actually estimated, but vcovHC() throws an error: "Error in bread. %*% meat. : non-conformable arguments". (The actual data I'm using is a bit more complicated. In fact, it is a model using two different fixed effects and I run into local singularities which I cannot

Use a weights argument in a list of lm lapply calls [duplicate]

一曲冷凌霜 提交于 2019-12-08 16:43:40
问题 This question already has answers here : Error in calling `lm` in a `lapply` with `weights` argument (2 answers) Closed last year . Here is my problem (fictional data in order to be reproducible) : set.seed(42) df<-data.frame("x"=rnorm(1000),"y"=rnorm(1000),"z"=rnorm(1000)) df2<-data.frame("x"=rnorm(100),"y"=rnorm(100),"z"=rnorm(100)) breaks<-c(-1000,-0.68,-0.01315,0.664,1000) divider<-cut(df$x,breaks) divider2<-cut(df2$x,breaks) subDF<-by(df,INDICES=divider,data.frame) subDF2<-by(df2,INDICES

Interpreting estimates of categorical predictors in linear regression [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-08 11:44:53
问题 This question already has answers here : Interpreting interactions in a regression model (2 answers) Closed 3 years ago . I'm new to linear regression and I'm trying to figure out how to interpret the summary results. I'm having difficulty interpreting the estimates of categorical predictors. Consider the following example. I added the columns age and length to include a numeric predictor and numeric target. library(MASS) data <- as.data.frame(HairEyeColor) data$length <- c(155, 173, 172, 176

R Variable Length Differ when build linear model for residuals

╄→尐↘猪︶ㄣ 提交于 2019-12-08 08:15:12
问题 I am working on a problem where I want to build a linear model using residuals of two other linear models. I have used UN3 data set to show my problem since its easy put the problem here than using my actual data set. Here is my R code: head(UN3) m1.lgFert.purban <- lm(log(Fertility) ~ Purban, data=UN3) m2.lgPPgdp.purban <- lm(log(PPgdp) ~ Purban, data=UN3) m3 <- lm(residuals(m1.lgFert.purban) ~ residuals(m2.lgPPgdp.purban)) Here is the error I am getting: > m3 <- lm(residuals(m1.lgFert

Ellipsis Trouble: Passing … to lm

无人久伴 提交于 2019-12-08 07:56:44
问题 I am building a wrapper around lm to do some additional calculations. I'd like the wrapper to pass ... to lm , but I am getting into trouble with lm 's weights argument. LmWrapper <- function(df, fmla, ...) { est <- lm(fmla, df, ...) list(model = est) } If I call the wrapper with a weights argument, data(airquality) LmWrapper(airquality, Ozone ~ Wind, weights = Temp) R does not know where to look for the weights: Error in eval(expr, envir, enclos) : ..1 used in an incorrect context, no ... to

Right way to use lm in R

有些话、适合烂在心里 提交于 2019-12-08 07:16:25
问题 I do not have very clear idea of how to use functions like lm() that ask for a formula and a data.frame. On the web I red about different approach but sometimes R give us warnings and other stuff Suppose for example a linear model where the output vector y is explained by the matrix X. I red that the best way is to use a data.frame (expecially if we are going to use the predict function later). In situation where the X is a matrix is this the best way to use lm? n=100 p=20 n_new=50 X=matrix

Shortcut using lm() in R for formula

我的未来我决定 提交于 2019-12-08 04:00:36
问题 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