-
Notifications
You must be signed in to change notification settings - Fork 1
/
elimination.R
21 lines (20 loc) · 974 Bytes
/
elimination.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Regress the given variable on the given predictor,
# suppressing the intercept, and return the residual.
regressOneOnOne <- function(predictor, other, dataframe){
# Point A. Create a formula such as Girth ~ Height -1
formula <- paste0(other, " ~ ", predictor, " - 1")
# Use the formula in a regression and return the residual.
resid(lm(formula, dataframe))
}
# Eliminate the specified predictor from the dataframe by
# regressing all other variables on that predictor
# and returning a data frame containing the residuals
# of those regressions.
eliminate <- function(predictor, dataframe){
# Find the names of all columns except the predictor.
others <- setdiff(names(dataframe), predictor)
# Calculate the residuals of each when regressed against the given predictor
temp <- sapply(others, function(other)regressOneOnOne(predictor, other, dataframe))
# sapply returns a matrix of residuals; convert to a data frame and return.
as.data.frame(temp)
}