Repository navigation
Expand file tree
/
Copy pathMethods.R
More file actions
70 lines (51 loc) · 2.36 KB
/
Copy pathMethods.R
File metadata and controls
70 lines (51 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Here we will be putting wrappers for implementing the methods
# Overall wrapper for each method should have the same form
# Input: vector Y, matrix X
# Output: estimated beta0 and beta
# Ridge regression
# We will use glmnet package with alpha = 0
#' @return hatbeta0, hatbeta
applyRidge <- function(Y, X){
#Cross validation is applied to select the best lambda
cv.out <- cv.glmnet(X, Y, alpha = 0, nfold = 5)
#This is extracting the best lambda
bestlam = cv.out$lambda.min
#Applying ridge regression with the best lambda that was selected
out <- glmnet(X, Y, alpha = 0, lambda = bestlam)
#Getting the beta values
ridge.coef <- as.vector(predict(out, type = "coefficients", s = bestlam)[1:(ncol(X)+1), ])
#Extracting the beta values for better output format
hatbeta0 <- ridge.coef[1]
hatbeta <- ridge.coef[2:length(ridge.coef)]
return(list(hatbeta0 = hatbeta0, hatbeta = hatbeta))
}
# Lasso regression
# We will use glmnet package with alpha = 1
#' @return hatbeta0, hatbeta
applyLasso <- function(Y, X){
#Extracts tuning parameter bestlam
cv.out <- cv.glmnet(X, Y, alpha = 1, nfold = 5)
# Uses cross validation to find the tuning parameter
bestlam = cv.out$lambda.min
#Gets coefficients for hatbeta0 and hatbeta values and sets using bestlam
out <- glmnet(X, Y, alpha = 1, lambda = bestlam)
lasso.coef <- as.vector(predict(out, type = "coefficients", s = bestlam)[1:(ncol(X)+1), ])
hatbeta0 <- lasso.coef[1]
hatbeta <- lasso.coef[2:length(lasso.coef)]
return(list(hatbeta0 = hatbeta0, hatbeta = hatbeta))
}
# PCR
# We will use pls package function pcr with CV (have to set the folds)
# Reference- https://statisticaloddsandends.wordpress.com/2018/10/15/obtaining-the-number-of-components-from-cross-validation-of-principal-components-regression/
#' @return hatbeta0, hatbeta chose from PCR
applyPCR <- function(Y, X){
#Finds the number of components by 5 fold cross validation
pcr.validate <- pcr(Y ~ X, validation = "CV", segments = 5, scale = TRUE)
lowest.error <- which.min(RMSEP(pcr.validate)$val[1,,]) - 1
#Gets the coefficients of model with lowest cross validation error
pcr.model <- pcr(Y ~ X, ncomp = lowest.error, scale = TRUE)
hatbeta <- as.vector(coef(pcr.model, intercept = TRUE))
hatbeta0 <- hatbeta[1]
hatbeta <- hatbeta[2:length(hatbeta)]
return(list(hatbeta0 = hatbeta0, hatbeta = hatbeta))
}