lm

Get hat matrix from QR decomposition for weighted least square regression

一笑奈何 提交于 2019-12-18 17:15:33
问题 I am trying to extend the lwr() function of the package McSptial , which fits weigthed regressions as non-parametric estimation. In the core of the lwr() function, it inverts a matrix using solve() instead of a QR decomposition, resulting in numerical instability. I would like to change it but can't figure out how to get the hat matrix (or other derivatives) from the QR decomposition afterward. With data : set.seed(0); xmat <- matrix(rnorm(500), nrow=50) ## model matrix y <- rowSums(rep(2:11

Get hat matrix from QR decomposition for weighted least square regression

纵然是瞬间 提交于 2019-12-18 17:15:22
问题 I am trying to extend the lwr() function of the package McSptial , which fits weigthed regressions as non-parametric estimation. In the core of the lwr() function, it inverts a matrix using solve() instead of a QR decomposition, resulting in numerical instability. I would like to change it but can't figure out how to get the hat matrix (or other derivatives) from the QR decomposition afterward. With data : set.seed(0); xmat <- matrix(rnorm(500), nrow=50) ## model matrix y <- rowSums(rep(2:11

Piecewise regression with a quadratic polynomial and a straight line joining smoothly at a break point

南楼画角 提交于 2019-12-18 16:59:38
问题 I want to fit a piecewise linear regression with one break point xt , such that for x < xt we have a quadratic polynomial and for x >= xt we have a straight line. Two pieces should join smoothly, with continuity up to 1st derivative at xt . Here's picture of what it may look like: I have parametrize my piecewise regression function as: where a , b , c and xt are parameters to be estimated. I want to compare this model with a quadratic polynomial regression over the whole range in terms of

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

筅森魡賤 提交于 2019-12-18 09:17:27
问题 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),

Error in dataframe *tmp* replacement has x data has y

放肆的年华 提交于 2019-12-18 07:44:12
问题 I'm a beginner in R. Here is a very simple code where I'm trying to save the residual term: # Create variables for child's EA: dat$cldeacdi <- rowMeans(dat[,c('cdcresp', 'cdcinv')],na.rm=T) dat$cldeacu <- rowMeans(dat[,c('cucresp', 'cucinv')],na.rm=T) # Create a residual score for child EA: dat$cldearesid <- resid(lm(cldeacu ~ cldeacdi, data = dat)) I'm getting the following message: Error in `$<-.data.frame`(`*tmp*`, cldearesid, value = c(-0.18608488908881, : replacement has 366 rows, data

Error in calling `lm` in a `lapply` with `weights` argument

牧云@^-^@ 提交于 2019-12-18 07:42:53
问题 I've encounter a weird behavior when calling lm within a lapply using the weights argument. My code consist of a list of formula on which I run a linear model that I call in lapply . So far it was working: dd <- data.frame(y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100), x3 = rnorm(100), x4 = rnorm(100), wg = runif(100,1,100)) ls.form <- list( formula(y~x1+x2), formula(y~x3+x4), formula(y~x1|x2|x3), formula(y~x1+x2+x3+x4) ) res.no.wg <- lapply(ls.form, lm, data = dd) However, when I add the

predict.lm() in a loop. warning: prediction from a rank-deficient fit may be misleading

我是研究僧i 提交于 2019-12-17 22:18:45
问题 This R code throws a warning # Fit regression model to each cluster y <- list() length(y) <- k vars <- list() length(vars) <- k f <- list() length(f) <- k for (i in 1:k) { vars[[i]] <- names(corc[[i]][corc[[i]]!= "1"]) f[[i]] <- as.formula(paste("Death ~", paste(vars[[i]], collapse= "+"))) y[[i]] <- lm(f[[i]], data=C1[[i]]) #training set C1[[i]] <- cbind(C1[[i]], fitted(y[[i]])) C2[[i]] <- cbind(C2[[i]], predict(y[[i]], C2[[i]])) #test set } I have a training data set (C1) and a test data set

convert string back into object in r [closed]

你说的曾经没有我的故事 提交于 2019-12-17 21:22:28
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last year . In R, I get the content of a binary file as a string (because of design issues, I can't access the file directly). This file was originally an lm model. How do I convert that string back into the lm model? Thanks 回答1: I'm assuming you used base::dput() according to the following example (based on

Linear models in R with different combinations of variables

杀马特。学长 韩版系。学妹 提交于 2019-12-17 19:46:57
问题 I am new to R and I am stuck with a problem. I am trying to read a set of data in a table and I want to perform linear modeling. Below is how I read my data and my variables names: >data =read.table(datafilename,header=TRUE) >names(data) [1] "price" "model" "size" "year" "color" What I want to do is create several linear models using different combinations of the variables (price being the target ), such as: > attach(data) > model1 = lm(price~model+size) > model2 = lm(price~model+year) >

Why does summary overestimate the R-squared with a “no-intercept” model formula

懵懂的女人 提交于 2019-12-17 18:48:05
问题 I wanted to make a simple linear model ( lm() ) without intercept coefficient so I put -1 in my model formula as in the following example. The problem is that the R-squared return by summary(myModel) seems to be overestimated. lm() , summary() and -1 are among the very classic function/functionality in R. Hence I am a bit surprised and I wonder if this is a bug or if there is any reason for this behaviour. Here is an example: x <- rnorm(1000, 3, 1) mydf <- data.frame(x=x, y=1+x+rnorm(1000, 0,