Skip to content

Instantly share code, notes, and snippets.

@adibenc
Created October 6, 2021 13:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save adibenc/ee7178812085a5f0e76f53adfd6f0c19 to your computer and use it in GitHub Desktop.
mat pca uts.r
library(ggplot2)
cr = function(d){
print(c(
nrow(d), ncol(d)
))
}
ple = function(d){
print(length(d))
}
printx = function(d){
print(d)
quit()
}
hr = function(d){
print(paste("================",d,"================"))
}
data = matrix(
c(
2.5, 2.4,
0.5, 0.7,
2.2, 2.9,
1.9, 2.2,
3.1, 3,
2.3, 2.7,
2, 1.6,
1, 1.1,
1.5, 1.6,
1.1, 0.9
),
ncol=2,
byrow=T
)
pca = function(data=NULL){
obj = list(
data=data,
x=NULL,
adjustData=NULL,
e=NULL,
rowFeatVector = NULL,
rowZeroMeanData = NULL,
finalData = NULL,
rowOriginalData = NULL
)
getcovars = function(m){
n = ncol(m)
covars = c()
for (i in 1: n){
for ( j in 1:n) {
# print(cov(m[,i], m[,j]))
covars = append(covars, cov(m[,i], m[,j]))
}
}
covars
}
adjustData = function(m){
n = ncol(m)
means = c()
for (i in 1:n){
me = mean(m[,i])
means = append(means, me)
m[,i] = m[,i] - me
}
list(
m=m,
means=means
)
}
covars = getcovars(data)
x = matrix(
covars,
# c(
# 0.616555556, 0.615444444,
# 0.615444444, 0.716555556
# ),
nrow=ncol(data),
byrow=T
)
obj$x = x
e = eigen(x, T)
obj$e = e
hr("vector")
print(e$vector)
print(t(e$vector))
objad = adjustData(data)
adjData = objad$m
# hr("adjdata")
# print(adjData)
# print(t(adjData))
obj$adjustData = adjData
obj$rowFeatVector = t(adjData)
obj$rowZeroMeanData = t(e$vector)
# finalData = rowFeatVector %*% rowZeroMeanData
obj$finalData = obj$rowZeroMeanData %*% obj$rowFeatVector
print(obj$finalData[,1])
obj$rowOriginalData = t(obj$rowFeatVector) %*% obj$finalData + objad$means
objad$means
# finalData = t(finalData)
print(x)
print(e)
print(data)
plot(obj$finalData[1,], obj$finalData[1,])
print(t(obj$finalData))
print(obj$rowOriginalData)
obj
}
# pca1 = pca(data)
# pca1
fname = "/media/data1/_S2/grad-notes/1.3-mtk-stat/dummy/phising.csv"
# fname = "./dummy/phising.csv"
df = read.csv(fname)
x = as.matrix( subset(df, select=having_IP_Address:Statistical_report) )
x = x[1:30,]
head(x)
doPCA1 = function(x){
# https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/prcomp
pcaresult = prcomp(x)
plot(pcaresult)
summary(pcaresult)
}
doPCA1(x)
# pca(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment