LRMultiClass: Function that implements multi-class logistic regression.

Description Usage Arguments Value Examples

View source: R/MyLogistic_function.R

Description

Function that implements multi-class logistic regression.

Usage

1
LRMultiClass(X, y, Xt, yt)

Arguments

X

n x p training data, 1st column should be 1s to account for intercept

y

a vector of size n of class labels, from 0 to K-1

Xt

ntest x p testing data, 1st column should be 1s to account for intercept

yt

a vector of size ntest of test class labels, from 0 to K-1

Value

A list of two elements,

beta

p x K matrix of estimated beta values after numIter iterations

error_train

(numIter + 1) length vector of training error % at each iteration (+ starting value)

Examples

 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
X1 <- matrix(rnorm(50, -3, 1), 50, 1)
Y1 <- matrix(c(0), 50, 1)
X2 <- matrix(rnorm(50, 3, 1), 50, 1)
Y2 <- matrix(c(1), 50, 1)
X <- c(X1, X2)
Y <- c(Y1, Y2)
X <- as.matrix(X)
Y <- as.matrix(Y)
random <- sample(nrow(X))
X <- as.matrix(X[random, ]) # training data from two normal distributions
Y <- as.matrix(Y[random, ]) # class labels for training data
# creating test data
X1t <- matrix(rnorm(10, -3, 1), 10, 1)
Y1t <- matrix(c(0), 10, 1)
X2t <- matrix(rnorm(10, 3, 1), 10, 1)
Y2t <- matrix(c(1), 10, 1)
Xt <- c(X1t, X2t)
Yt <- c(Y1t, Y2t)
Xt <- as.matrix(Xt)
Yt <- as.matrix(Yt)
random <- sample(nrow(Xt))
Xt <- as.matrix(Xt[random, ]) # testing data from the same two normal distributions
Yt <- as.matrix(Yt[random, ]) # class labels for testing data
# adding columns of 1's to training and testing data
colX1 <- rep(1, nrow(X))
X <- cbind(colX1, X)
colXt1 <- rep(1, nrow(Xt))
Xt <- cbind(colXt1, Xt)
output <- LRMultiClass(X, Y, Xt, Yt)

gargjhanvi/SSLLabelGuide documentation built on Dec. 20, 2021, 9:48 a.m.