Multinomial Model Final Problem 2:

#install.packages("keras")
library(keras)
library(ggplot2)
library(grid)
library(gridExtra)
library(nnet)
trainpic <- read.csv("C://Users//malia//OneDrive//Desktop//data 605//train.csv")
testpic <- read.csv("C://Users//malia//OneDrive//Desktop//coop//test.csv")
m<-matrix(unlist(trainpic[11,-1]), nrow=28, byrow = T)
image(m, col=grey.colors(255))
digit<-function(x){
  m<-matrix(unlist(x), nrow=28, byrow=T)
  m<-t(apply(m, 2, rev))
  image(m, col=grey.colors(255))
}

par(mfrow=c(3,4))

for(i in 1:10){
  digit(trainpic[i, -1])
}
head(trainpic)

Cleaning Data:

# Transform data to be on a 0 - 1 scale
trainpic[,-1] <- (trainpic[,-1]/255)

Build Histogram

trainpic$label <- as.factor(trainpic$label)
trainpic$intensity <- apply(trainpic[,-1], 1, mean) #takes the mean of each row in train

intbylabel <- aggregate (trainpic$intensity, by = list(trainpic$label), FUN = mean)

plot <- ggplot(data=intbylabel, aes(x=Group.1, y = x)) +
    geom_histogram(stat="identity")
plot + scale_x_discrete(limits=0:9) + xlab("digit label") + 
    ylab("average intensity")

As we can see there are some differences in intensity. The digit “1” is the less intense while the digit “0” is the most intense. So this new feature seems to have some predictive value.

# Get the principal components from PCA
pca_output <- prcomp(trainpic[, -1])

# Observe a summary of the output
#summary(pca_output)

# Store the first ten coordinates and the label in a data frame
pca_plot <- data.frame(pca_x = pca_output$x[, 1], pca_y = pca_output$x[, 10], 
                       label = as.factor(trainpic$label))

# Plot the first ten principal components using the true labels as color and shape
ggplot(pca_plot, aes(x = pca_x, y = pca_y, color = label)) + 
    ggtitle("PCA of MNIST sample") + 
    geom_text(aes(label = label)) + 
    theme(legend.position = "none")
library(tidyverse)
library(dplyr)
eightpic <- trainpic %>% filter(label == 8)

For 8

# Get the principal components from PCA for 8
pca_outpute <- prcomp(eightpic[, -1])

# Observe a summary of the output
#summary(pca_outpute)

# Store the first ten coordinates and the label in a data frame for 8
pca_plot <- data.frame(pca_xe = pca_outpute$x[, 1], pca_ye = pca_outpute$x[, 10], 
                       label = as.factor(eightpic$label))

# Plot the first ten principal components using the true labels as color and shape
ggplot(pca_plot, aes(x = pca_xe, y = pca_ye, color = label)) + 
    ggtitle("PCA of eight MNIST sample") + 
    geom_text(aes(label = label)) + 
    theme(legend.position = "none")

multinomial model:

nrows_train<- nrow(trainpic)*0.75 
set.seed(5465485)
rows_train<- sample(1:nrow(trainpic),nrows_train)

pic_train<- trainpic[rows_train,]
pic_test<- trainpic[-rows_train,]

y<- as.numeric(unlist(pic_train[,1]))
X<- pic_train[, -1]

var_cols<- apply(data_train, 2, 'var')
pic_var_cols<- which(var_cols==0)

X<- pic_train[ , -c(no_var_cols, 1)]

pca_val<- prcomp(X, center=F, scale=F)
X_cov<- cov(X)
pca_cov<- prcomp(X_cov)
pca_covper_var<- pca_cov$sdev^2/sum(pca_cov$sdev^2)
X_loading<- pca_cov$rotation 


X_train<- as.matrix(data_train[, -c(pic_var_cols,1)]/max(data_train[,-1]))
X_train<- X_train %*% X_loading[,1:50]
y_train<- data_train[,'label']


X_test<- as.matrix(data_test[ ,-c(pic_var_cols,1)]/max(data_train[,-1]))
X_test<- X_test %*% X_loading[,1:50]
y_test<- data_test[,'label']



df.train<- data.frame(y=(y_train), X=X_train)
df.test<- data.frame(y=(y_test), X=X_test)
model_multinom<- nnet(y~., data=df.train,size=250, MaxNWts=100000, maxit=1000)
summary(model_multinom)