You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/v02_Evaluation_using_Trio.Rmd
+73-53Lines changed: 73 additions & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -77,74 +77,94 @@ Using the `CVindices`, user can subset to training and test data. As an example,
77
77
78
78
Once predictions are obtained for the test set, we pass them to Trio using the same evidence name as stored in the Trio object (i.e., `Diagnosis`). Specifically, we call `trio$evaluate(list(lasso = list(Diagnosis = pred)))`, which instructs `evaluate()` to compare `pred` against the reference labels `Diagnosis` stored in `trio`, and then compute the specified metric (Balanced Accuracy).
79
79
80
+
We first construct an explicit cross-validation plan that records fold and repeat identifiers, and then iterate over this plan to evaluate each split.
81
+
80
82
```{r}
81
83
set.seed(1234)
82
84
83
-
# Loop through 2 folds x 5 repeats = 10 runs
84
-
result <- do.call(
85
-
rbind,
86
-
mapply(
87
-
function(trainIDs, crossValID) {
88
-
x_train <- x[trainIDs, ]
89
-
x_test <- x[-trainIDs, ]
90
-
y_train <- y[trainIDs]
91
-
y_test <- y[-trainIDs]
92
-
93
-
# Find the best lambda for LASSO regression
94
-
cv_lasso <- cv.glmnet(
95
-
x = as.matrix(x_train),
96
-
y = y_train,
97
-
alpha = 1,
98
-
family = "binomial"
99
-
)
100
-
lam <- cv_lasso$lambda.1se
101
-
102
-
# Fit a model with the best lambda on training data
103
-
fit <- glmnet(
104
-
x = as.matrix(x_train),
105
-
y = y_train,
106
-
alpha = 1,
107
-
lambda = lam,
108
-
family = "binomial"
109
-
)
110
-
111
-
# Evaluate the model on test data
112
-
pred <- predict(
113
-
fit,
114
-
newx = as.matrix(x_test),
115
-
s = lam,
116
-
type = "class"
117
-
)
118
-
pred <- setNames(as.factor(as.vector(pred)), rownames(x_test))
0 commit comments