简体   繁体   中英

How do I set the weighed in linear regression model in R?

I have the following data in R and I want to get the linear regression model for y~x1+x2+x3+x4 using weighted least square. If I want to use the sample variance as the basis for weighted squares estimation of the original data.

Schubert et al. (1992) conducted an experiment with a catapult to determine the effects of hook ( x 1 ), arm length ( x 2 ), start angle ( x 3 ), and stop angle ( x 4 ) on the distance ( y ) that the catapult throws a ball. They throw the ball three times for each setting of the factors. The following table summarizes the experimental results, where the factor levels of x 1, x 2, x 3 and x 4 are standardized

x1<-c(-1,-1,-1,-1,1,1,1,1)
x2<-c(-1,-1,1,1,-1,-1,1,1)
x3<-c(-1,1,-1,1,-1,1,-1,1)
x4<-c(-1,1,1,-1,1,-1,-1,1)
y<-c(28,46.3,21.9,52.9,75,127.7,86.2,195)

How do I write this code in R? I tried the following one but I do not know how to set the weighted w in R. This code dose not work.

l=lm(y~x1+x2+x3+x4, weights=1/x) 

Nothwithstanding the lack of clarifications and details around your model and data (see below), I think you're after something like this:

# Store sample data in `data.frame`
df <- data.frame(y, x1, x2, x3, x4)

# First: "Vanilla" OLS estimation
fit_OLS <- lm(y ~ ., data = df)

# Second: Weights
weights <- 1 / fitted(lm(abs(residuals(fit_OLS)) ~ fitted(fit_OLS))) ^ 2

# Third: Weighted least squares estimation
fit_WLS <- lm(y ~  ., data = df, weights = weights)

# Compare coefficients
coef(fit_OLS)
#(Intercept)          x1          x2          x3          x4 
#     79.125      41.850       9.875      26.350       5.425 
coef(fit_WLS)
#(Intercept)          x1          x2          x3          x4 
#     79.125      41.850       9.875      26.350       5.425 

Not wanting to reiterate poorly what others have explained much better, I refer you to this post , detailing the rationale behind calculating the weights.

As you can see, parameter estimates of the OLS and WLS routines are the same, since the estimated weights are identical.

This loops back to my original question (see comments): The values for those x1 , x2 , x3 , x4 predictors seem odd. If they denote hook, arm length, start angle and stop angle of a catapult, why do they only take on values -1 and 1 ? Did you discretise original data somehow? This is not clear but important for assessing the model fit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM