lm

warning when calculating predicted values

我的梦境 提交于 2019-11-30 20:19:21
working with a data frame x Date Val 1/1/2012 7 2/1/2012 9 3/1/2012 20 4/1/2012 24 5/1/2012 50 a <- seq(as.Date(tail(x, 1)$Date), by="month", length=5) a <- data.frame(a) x.lm <- lm(x$Val ~ x$Date) x.pre<-predict(x.lm, newdata=a) I am getting this erro: Warning message: 'newdata' had 5 rows but variable(s) found have 29 rows what am I doing wrong? here is the dput output: dput(x) structure(list(Date = structure(c(14610, 14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14975, 15006, 15034, 15065, 15095, 15126, 15156, 15187, 15218, 15248, 15279, 15309, 15340, 15371,

Is there a function or package which will simulate predictions for an object returned from lm()?

大憨熊 提交于 2019-11-30 13:39:33
Is there a single function, similar to "runif", "rnorm" and the like which will produce simulated predictions for a linear model? I can code it on my own, but the code is ugly and I assume that this is something someone has done before. slope = 1.5 intercept = 0 x = as.numeric(1:10) e = rnorm(10, mean=0, sd = 1) y = slope * x + intercept + e fit = lm(y ~ x, data = df) newX = data.frame(x = as.numeric(11:15)) What I'm interested in is a function that looks like the line below: sims = rlm(1000, fit, newX) That function would return 1000 simulations of y values, based on the new x variables.

How to manually set coefficients for variables in linear model? [duplicate]

人走茶凉 提交于 2019-11-30 13:25:41
问题 This question already has an answer here : Set one or more of coefficients to a specific integer (1 answer) Closed 3 years ago . In R, how can I set weights for particular variables and not observations in lm() function? Context is as follows. I'm trying to build personal ranking system for particular products, say, for phones. I can build linear model based on price as dependent variable and other features such as screen size, memory, OS and so on as independent variables. I can then use it

Prediction of 'mlm' linear model object from `lm()`

穿精又带淫゛_ 提交于 2019-11-30 09:37:20
问题 I have three datasets: response - matrix of 5(samples) x 10(dependent variables) predictors - matrix of 5(samples) x 2(independent variables) test_set - matrix of 10(samples) x 10(dependent variables defined in response) response <- matrix(sample.int(15, size = 5*10, replace = TRUE), nrow = 5, ncol = 10) colnames(response) <- c("1_DV","2_DV","3_DV","4_DV","5_DV","6_DV","7_DV","8_DV","9_DV","10_DV") predictors <- matrix(sample.int(15, size = 7*2, replace = TRUE), nrow = 5, ncol = 2) colnames

Fitting linear model / ANOVA by group [duplicate]

安稳与你 提交于 2019-11-30 06:04:14
问题 This question already has answers here : Linear Regression and group by in R (10 answers) Closed 3 years ago . I'm trying to run anova() in R and running into some difficulty. This is what I've done up to now to help shed some light on my question. Here is the str() of my data to this point. str(mhw) 'data.frame': 500 obs. of 5 variables: $ r : int 1 2 3 4 5 6 7 8 9 10 ... $ c : int 1 1 1 1 1 1 1 1 1 1 ... $ grain: num 3.63 4.07 4.51 3.9 3.63 3.16 3.18 3.42 3.97 3.4 ... $ straw: num 6.37 6.24

Can't get aggregate() work for regression by group

北慕城南 提交于 2019-11-29 17:41:54
I want to use aggregate with this custom function: #linear regression f-n CalculateLinRegrDiff = function (sample){ fit <- lm(value~ date, data = sample) diff(range(fit$fitted)) } dataset2 = aggregate(value ~ id + col, dataset, CalculateLinRegrDiff(dataset)) I receive the error: Error in get(as.character(FUN), mode = "function", envir = envir) : object 'FUN' of mode 'function' was not found What is wrong? Your syntax on using aggregate is wrong in the first place. Pass function CalculateLinRegrDiff not an evaluated one CalculateLinRegrDiff(dataset) to FUN argument. Secondly, you've chosen the

Prediction of 'mlm' linear model object from `lm()`

[亡魂溺海] 提交于 2019-11-29 16:15:11
I have three datasets: response - matrix of 5(samples) x 10(dependent variables) predictors - matrix of 5(samples) x 2(independent variables) test_set - matrix of 10(samples) x 10(dependent variables defined in response) response <- matrix(sample.int(15, size = 5*10, replace = TRUE), nrow = 5, ncol = 10) colnames(response) <- c("1_DV","2_DV","3_DV","4_DV","5_DV","6_DV","7_DV","8_DV","9_DV","10_DV") predictors <- matrix(sample.int(15, size = 7*2, replace = TRUE), nrow = 5, ncol = 2) colnames(predictors) <- c("1_IV","2_IV") test_set <- matrix(sample.int(15, size = 10*2, replace = TRUE), nrow =

How to update `lm` or `glm` model on same subset of data?

北城以北 提交于 2019-11-29 16:00:45
I am trying to fit two nested models and then test those against each other using anova function. The commands used are: probit <- glm(grad ~ afqt1 + fhgc + mhgc + hisp + black + male, data=dt, family=binomial(link = "probit")) nprobit <- update(probit, . ~ . - afqt1) anova(nprobit, probit, test="Rao") However, the variable afqt1 apparently contains NA s and because the update call does not take the same subset of data, anova() returns error Error in anova.glmlist(c(list(object), dotargs), dispersion = dispersion, : models were not all fitted to the same size of dataset Is there a simple way

r functions calling lm with subsets

随声附和 提交于 2019-11-29 15:23:36
I was working on some code and I noticed something peculiar. When I run LM on a subset of some panel data I have it works fine, something like this: library('plm') data(Cigar) lm(log(price) ~ log(pop) + log(ndi), data=Cigar, subset=Cigar$state==1) Call: lm(formula = log(price) ~ log(pop) + log(ndi), data = Cigar, subset = Cigar$state == 1) Coefficients: (Intercept) log(pop) log(ndi) -26.4919 3.2749 0.4265 but when I try to wrap this in a function I get: myfunction <- function(formula, data, subset){ return(lm(formula, data, subset)) } myfunction(formula = log(price) ~ log(pop) + log(ndi), data

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

萝らか妹 提交于 2019-11-29 14:50:12
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 if I could store a list of lm objects or a matrix with statistics of interest. Again, the objective is