In the following code nbest indicates the number of subsets of each size to report. stepAIC( ) performs stepwise model selection by exact AIC.Īlternatively, you can perform all-subsets regression using the leaps( ) function from the leaps package. You can perform stepwise selection (forward, backward, both) using the stepAIC( ) function from the MASS package. Selecting a subset of predictor variables from a larger set (e.g., stepwise selection) is a controversial topic. Results <- crossval(X,y,theta.fit,theta.predict,ngroup=10)Ĭor(y,results$cv.fit)**2 # cross-validated R2 Variable Selection # Assessing R2 shrinkage using 10-Fold Cross-Validation Using the crossval() function from the bootstrappackage, do the following: You can assess R2 shrinkage via K-fold cross-validation. Sum the MSE for each fold, divide by the number of observations, and take the square root to get the cross-validated standard error of estimate. You can do K-Fold cross-validation using the cv.lm( ) function in the DAAG package.Ĭv.lm(df=mydata, fit, m=3) # 3 fold cross-validation The following code provides a simultaneous test that x3 and x4 add to linear prediction above and beyond x1 and x2.įit1 <- lm(y ~ x1 + x2 + x3 + x4, data=mydata) You can compare nested models with the anova( ) function. Layout(matrix(c(1,2,3,4),2,2)) # optional 4 graphs/pageįor a more comprehensive evaluation of model fit see regression diagnostics or the exercises in this interactive course on regression. Vcov(fit) # covariance matrix for model parametersĭiagnostic plots provide checks for heteroscedasticity, normality, and influential observerations. Fitting the ModelĬonfint(fit, level=0.95) # CIs for model parameters The topics below are provided in order of increasing complexity. R provides comprehensive support for multiple linear regression.
0 Comments
Leave a Reply. |