lm

Datatype for linear model in R

╄→гoц情女王★ 提交于 2019-12-17 17:22:55
问题 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

r predict function returning too many values [closed]

我们两清 提交于 2019-12-17 16:53:21
问题 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've read other postings regarding named variables and tried implementing the answers but still get too many values for my new data that I want to run my existing model on. Here is working example code: set.seed(123) mydata <- data.frame("y"=rnorm(100,mean=0, sd = 1),"x"=c(1:100)) mylm <- lm(y ~ x, data=mydata)

how to debug “factor has new levels” error for linear model and prediction

若如初见. 提交于 2019-12-17 13:36:19
问题 I am trying to make and test a linear model as follows: lm_model <- lm(Purchase ~., data = train) lm_prediction <- predict(lm_model, test) This results in the following error, stating that the Product_Category_1 column has values that exist in the test data frame but not the train data frame): factor Product_Category_1 has new levels 7, 9, 14, 16, 17, 18 However, if I check these they definitely look to appear in both data frames: > nrow(subset(train, Product_Category_1 == "7")) [1] 2923 >

how to debug “factor has new levels” error for linear model and prediction

和自甴很熟 提交于 2019-12-17 13:34:09
问题 I am trying to make and test a linear model as follows: lm_model <- lm(Purchase ~., data = train) lm_prediction <- predict(lm_model, test) This results in the following error, stating that the Product_Category_1 column has values that exist in the test data frame but not the train data frame): factor Product_Category_1 has new levels 7, 9, 14, 16, 17, 18 However, if I check these they definitely look to appear in both data frames: > nrow(subset(train, Product_Category_1 == "7")) [1] 2923 >

Showing string in formula and not as variable in lm fit

╄→гoц情女王★ 提交于 2019-12-17 10:09:08
问题 I am not able to resolve the issue that when lm(sformula) is executed, it does not show the string that is assigned to sformula . I have a feeling it is generic way R handles argument of a function and not specific to linear regression. Below is the illustration of the issue through examples. Example 1, has the undesired output lm(formula = sformula) . The example 2 is the output I would like i.e., lm(formula = "y~x") . x <- 1:10 y <- x * runif(10) sformula <- "y~x" ## Example: 1 lm(sformula)

Messy plot when plotting predictions of a polynomial regression using lm() in R

北城余情 提交于 2019-12-17 10:06:32
问题 I am building a quadratic model with lm in R: y <- data[[1]] x <- data[[2]] x2 <- x^2 quadratic.model = lm(y ~ x + x2) Now I want to display both the predicted values and the actual values on a plot. I tried this: par(las=1,bty="l") plot(y~x) P <- predict(quadratic.model) lines(x, P) but the line comes up all squiggely. Maybe it has to do with the fact that it's quadratic? Thanks for any help. 回答1: You need order() : P <- predict(quadratic.model) plot(y~x) reorder <- order(x) lines(x[reorder]

Messy plot when plotting predictions of a polynomial regression using lm() in R

陌路散爱 提交于 2019-12-17 10:05:16
问题 I am building a quadratic model with lm in R: y <- data[[1]] x <- data[[2]] x2 <- x^2 quadratic.model = lm(y ~ x + x2) Now I want to display both the predicted values and the actual values on a plot. I tried this: par(las=1,bty="l") plot(y~x) P <- predict(quadratic.model) lines(x, P) but the line comes up all squiggely. Maybe it has to do with the fact that it's quadratic? Thanks for any help. 回答1: You need order() : P <- predict(quadratic.model) plot(y~x) reorder <- order(x) lines(x[reorder]

Set one or more of coefficients to a specific integer

青春壹個敷衍的年華 提交于 2019-12-17 07:47:52
问题 I am using a standard lm model and would like to set the coefficients of one or more of my variables to a specific integer. For example, I would like the coefficient of my weather and price variables to be 647 and 15 respectively. I am using the lm function with a standard formula. The closest things I've found so far are the offset function within glm, or restrict.rhs within systemfit. I've also looked at subtracting the total contribution from these variables with their coefficients set,

How do I create a “macro” for regressors in R?

喜你入骨 提交于 2019-12-17 06:51:52
问题 For long and repeating models I want to create a "macro" (so called in Stata and there accomplished with global var1 var2 ... ) which contains the regressors of the model formula. For example from library(car) lm(income ~ education + prestige, data = Duncan) I want something like: regressors <- c("education", "prestige") lm(income ~ @regressors, data = Duncan) I could find is this approach. But my application on the regressors won't work: reg = lm(income ~ bquote(y ~ .(regressors)), data =

Predict() - Maybe I'm not understanding it

試著忘記壹切 提交于 2019-12-16 19:52:12
问题 I posted earlier today about an error I was getting with using the predict function. I was able to get that corrected, and thought I was on the right path. I have a number of observations (actuals) and I have a few data points that I want to extrapolate or predict. I used lm to create a model, then I tried to use predict with the actual value that will serve as the predictor input. This code is all repeated from my previous post, but here it is: df <- read.table(text = ' Quarter Coupon Total