The process to perform PaCMAP dimension reduction using Reduction Wrappers in R is as follows:
library(ConjointChecks)
library(Rtsne)
library(pamr)
library(Ramd)
library(dplyr)
library(Rtsne)
data <- read.csv("my_data.csv")
set.seed(123)
train_indices <- sample(nrow(data), 0.7 * nrow(data))
train_data <- data[train_indices, ]
test_data <- data[-train_indices, ]
createReductionWrapper()
function:reduction_wrapper <- createReductionWrapper(
reduction = "pamr",
n_components = 2,
verbose = TRUE,
uses_labels = TRUE
)
In the above code, we are creating a reduction wrapper object that uses the "pamr" algorithm to perform dimension reduction, and reduces the data to 2 dimensions. We have set the uses_labels
parameter to TRUE
, indicating that the algorithm will use the class labels available in the dataset to perform supervised dimension reduction.
trainReductionWrapper()
function:reduction_wrapper <- trainReductionWrapper(
reduction_wrapper,
train_data[, -1], # exclude the response variable
train_data[, 1] # class labels
)
In the above code, we are training the reduction wrapper on the training dataset using the trainReductionWrapper()
function. We are passing the training data without the response variable (the first column of the dataset), and the class labels (the first column of the dataset) to the function.
transformReductionWrapper()
function:train_data_reduced <- transformReductionWrapper(reduction_wrapper, train_data[, -1])
test_data_reduced <- transformReductionWrapper(reduction_wrapper, test_data[, -1])
In the above code, we are transforming the training and testing datasets using the transformReductionWrapper()
function. We are passing the training and testing data without the response variable (the first column of the dataset) to the function.
model <- glm(response_variable ~ ., data = train_data_reduced, family = binomial)
In the above code, we are using the reduced training dataset to build a logistic regression model using the glm()
function.
predicted <- predict(model, newdata = test_data_reduced, type = "response")
confusion_matrix <- table(test_data[, 1], predicted > 0.5)
In the above code, we are predicting the class labels for the testing dataset using the predict()
function, and creating a confusion matrix using the table()
function.
That's it! We have successfully performed PaCMAP dimension reduction using Reduction Wrappers in R.
Asked: 2021-05-26 11:00:00 +0000
Seen: 12 times
Last updated: Dec 28 '22