lm

Plotting conditional density of prediction after linear regression

情到浓时终转凉″ 提交于 2019-12-12 20:09:30
问题 This is my data frame: data <- structure(list(Y = c(NA, -1.793, -0.642, 1.189, -0.823, -1.715, 1.623, 0.964, 0.395, -3.736, -0.47, 2.366, 0.634, -0.701, -1.692, 0.155, 2.502, -2.292, 1.967, -2.326, -1.476, 1.464, 1.45, -0.797, 1.27, 2.515, -0.765, 0.261, 0.423, 1.698, -2.734, 0.743, -2.39, 0.365, 2.981, -1.185, -0.57, 2.638, -1.046, 1.931, 4.583, -1.276, 1.075, 2.893, -1.602, 1.801, 2.405, -5.236, 2.214, 1.295, 1.438, -0.638, 0.716, 1.004, -1.328, -1.759, -1.315, 1.053, 1.958, -2.034, 2.936,

R: Error in contrasts when fitting linear models with `lm`

徘徊边缘 提交于 2019-12-12 17:16:47
问题 I've found Error in contrasts when defining a linear model in R and have followed the suggestions there, but none of my factor variables take on only one value and I am still experiencing the same issue. This is the dataset I'm using: https://www.dropbox.com/s/em7xphbeaxykgla/train.csv?dl=0. This is the code I'm trying to run: simplelm <- lm(log_SalePrice ~ ., data = train) #Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : # contrasts can be applied only to factors with 2

How to drop NA observation of factors conditionally when doing linear regression in R?

有些话、适合烂在心里 提交于 2019-12-12 15:23:04
问题 I'm trying to do a simple linear regression model in R. there are three factor variables in the model. the model is lm(Exercise ~ Econ + Job + Position) where "Exercise" is numeric dependent variable, the amount of time exercising. "Econ", "Job", "Position" are all factor variables. "Econ" is whether a person is employed or not. (levels = employed / not employed) "Job" is the job type a person has. There are five levels for this variable. "Position" is the position a person has in the

Combining cbind and paste in linear model

别说谁变了你拦得住时间么 提交于 2019-12-12 10:56:24
问题 I would like to know how can I come up with a lm formula syntax that would enable me to use paste together with cbind for multiple multivariate regression. Example In my model I have a set of variables, which corresponds to the primitive example below: data(mtcars) depVars <- paste("mpg", "disp") indepVars <- paste("qsec", "wt", "drat") Problem I would like to create a model with my depVars and indepVars . The model, typed by hand, would look like that: modExmple <- lm(formula = cbind(mpg,

Custom R function around plot_ly() with fitted(lm(y~x)) using add_lines()

三世轮回 提交于 2019-12-12 05:59:15
问题 I want to write a custom function around plot_ly() in R. That way, I can make a series of scatterplots with the same formatting and style, but not duplicate code. I used this page as a guide. This code reproduces the error: library(plotly) my_plot <- function(x, y, ...) { require(broom) plot_ly(data = mtcars, y = y, x = x, showlegend = FALSE, ...) %>% add_markers(y = y) %>% add_lines(y = ~fitted(lm(y ~ x))) %>% add_ribbons(data = augment(lm(y ~ x, data = mtcars)), ymin = ~.fitted - 1.96 * .se

Error when building regression model using lm ( Error in `contrasts<-`(`*tmp*`… contrasts can be applied only to factors with 2 or more levels) [duplicate]

走远了吗. 提交于 2019-12-12 05:44:58
问题 This question already has answers here : How to debug “contrasts can be applied only to factors with 2 or more levels” error? (2 answers) Closed last year . I get this error depending on which variables I include and the sequence in which I specify them in the formula: Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels I've done a little research on this and it looks like it would be caused by the variable in

R : constraining coefficients and error variance over multiple subsample regressions [closed]

家住魔仙堡 提交于 2019-12-12 03:33:36
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I'm working with R on a sample of 145 observations. I have created five subsamples each with 29 observations, while the response variable q has been sorted. As a result, subset1 contains the 29 lines of the data frame with the lowest output, subset2 contains the following 29 lines, etc. I am regressing the

How to specify lm model matrix

和自甴很熟 提交于 2019-12-12 03:22:44
问题 I have measurements obtained from 2 groups where each group has the same 3 levels. Here's my example data.frame : df <- data.frame(measurement = c(rnorm(10,1,1),rnorm(10,0.75,1),rnorm(10,1.25,1), rnorm(10,0.5,1),rnorm(10,1.75,1),rnorm(10,0.25,1)), group = as.factor(c(rep("a",30),rep("b",30))), level = as.factor(rep(c(rep("x",10),rep("y",10),rep("z",10)),2))) I'm interested in quantifying how measurement in each level is affected by group . I guess a linear model ( lm ) is the appropriate

Show residuals with speedlm

丶灬走出姿态 提交于 2019-12-12 02:46:06
问题 Due to the size of my dataset I'm bound to use Speedlm, fastLm or biglm. Unfortunately I'm stuck to using speedlm as fastlm doesn't have an update function, and biglm only supports single core. Using speedlm I want to show all residuals. I know that for lm or fastlm I can simply use the residuals() function. However it turns out speedlm doesn't support this. lmfit <- speedglm(formula , res) print(names(lmfit)) [1] "coefficients" "coef" "df.residual" "XTX" "Xy" "nobs" "nvar" "ok" "A" "RSS"

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

隐身守侯 提交于 2019-12-12 00:34:15
问题 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 29 ... $