Continuación clase anterior Discriminación y Clasificación
library(readxl)
datos = read_excel("G:/Mi unidad/MAESTRÍA CIENCIA Y TECNOLOGÍA ALIMENTOS/Métodos Multivariados/Clases/discriminantes_051923.xlsx")
View(datos)
library(rgl)
## Warning: package 'rgl' was built under R version 4.2.3
plot3d(datos,type = 's')
Se particiona la matriz 80% datos entrenamiento y 20% con datos de prueba, por tanto la matriz de entrenamiento tiene 80 filas y la de prueba 20 filas, luego los datos se meten en el modelo para ver como funcionan
#Costos clasificados 1(dado que) pertenece a 2
C12 = 1
#Costos clasificados 2(dado que) pertenece a 1
C21 = 2
f_disc <- function(X, g, X0, c12, c21){
tbl = table(g)
n1 = tbl[1]
n2 = tbl[2]
Xg = split(X, g)
X1 = Xg[[1]]
X2 = Xg[[2]]
p1 = (n1) / (n1+n2)
p2 = (n2) / (n1+n2)
mu1 = colMeans(X1)
S1 = cov(X1)
mu2 = colMeans(X2)
S2 = cov(X2)
SP = ((n1-1)/(n1-1 + n2-1))*S1 + ((n1-2)/(n1-1 + n2-1))*S2
izq = t(mu1-mu2)%*%solve(SP)%*%t(X0) - (1/2)*t(mu1-mu2)%*%solve(SP)%*%(mu1+mu2)
der = log((c12/c21)*(p2/p1))
clas = ifelse(izq>=der, names(tbl)[1], names(tbl)[2])
return(clas)
}
library(faux)
## Warning: package 'faux' was built under R version 4.2.3
##
## ************
## Welcome to faux. For support and examples visit:
## https://debruine.github.io/faux/
## - Get and set global package options with: faux_options()
## ************
set.seed(123)
loc1 = rnorm_multi(
n = 50, vars = 3,
mu = c(20, 30, 2),
sd = c(2, 3, 0.2),
r = 0.7,
varnames = c('A','Ar','MO'))
loc2 = rnorm_multi(
n = 50, vars = 3,
mu = c(23, 33, 2.4),
sd = c(2, 3, 0.2),
r = 0.8,
varnames = c('A','Ar','MO'))
loc3 = rnorm_multi(
n = 50, vars = 3,
mu = c(25, 35, 2.7),
sd = c(2, 3, 0.2),
r = 0.76,
varnames = c('A','Ar','MO'))
datos = rbind(loc1, loc2, loc3)
datos$loc=gl(3,50,150)
rgl::plot3d(datos$A,
datos$Ar,
datos$MO,
col=datos$loc,
type='s')
library(caret)
## Warning: package 'caret' was built under R version 4.2.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.2.3
## Loading required package: lattice
metodos = c('lda2', 'stepLDA')
model = lapply(metodos, function(mi){
train(x = datos[, -c(3,4)],
y = datos[, 4],
method = mi)
})
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.68; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.0 0.0 0.1
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.62667; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.11
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.64667; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.11
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.66; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.11
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.68; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.09
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.67333; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.08
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.62667; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.0 0.0 0.1
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.68667; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.0 0.0 0.1
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.64; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.11
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.68667; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.13
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.64667; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.09
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.72667; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.11
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.61333; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.0 0.0 0.1
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.70667; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.09
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.64; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.09
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.63333; in: "Ar"; variables (1): Ar
##
## hr.elapsed min.elapsed sec.elapsed
## 0.0 0.0 0.1
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.68; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.0 0.0 0.1
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.69333; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.09
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.56; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.0 0.0 0.1
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.73333; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.09
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.66667; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.0 0.0 0.1
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.68; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.09
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.72; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.0 0.0 0.1
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.73333; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.00 0.00 0.09
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.68; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.0 0.0 0.1
## `stepwise classification', using 10-fold cross-validated correctness rate of method lda'.
## 150 observations of 2 variables in 3 classes; direction: both
## stop criterion: improvement less than 5%.
## correctness rate: 0.68667; in: "A"; variables (1): A
##
## hr.elapsed min.elapsed sec.elapsed
## 0.0 0.0 0.1
model[[1]]$method
## [1] "lda2"
model[[2]]$method
## [1] "stepLDA"
y_pred_m1=predict(model[[1]],
datos[,-4])
y_pred_m2=predict(model[[2]],
datos[,-4])
confusionMatrix(data = y_pred_m1,
reference = datos$loc)
## Confusion Matrix and Statistics
##
## Reference
## Prediction 1 2 3
## 1 39 10 4
## 2 10 29 11
## 3 1 11 35
##
## Overall Statistics
##
## Accuracy : 0.6867
## 95% CI : (0.6059, 0.7598)
## No Information Rate : 0.3333
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.53
##
## Mcnemar's Test P-Value : 0.6149
##
## Statistics by Class:
##
## Class: 1 Class: 2 Class: 3
## Sensitivity 0.7800 0.5800 0.7000
## Specificity 0.8600 0.7900 0.8800
## Pos Pred Value 0.7358 0.5800 0.7447
## Neg Pred Value 0.8866 0.7900 0.8544
## Prevalence 0.3333 0.3333 0.3333
## Detection Rate 0.2600 0.1933 0.2333
## Detection Prevalence 0.3533 0.3333 0.3133
## Balanced Accuracy 0.8200 0.6850 0.7900
confusionMatrix(data = y_pred_m2,
reference = datos$loc)
## Confusion Matrix and Statistics
##
## Reference
## Prediction 1 2 3
## 1 39 10 4
## 2 10 28 11
## 3 1 12 35
##
## Overall Statistics
##
## Accuracy : 0.68
## 95% CI : (0.599, 0.7537)
## No Information Rate : 0.3333
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.52
##
## Mcnemar's Test P-Value : 0.6055
##
## Statistics by Class:
##
## Class: 1 Class: 2 Class: 3
## Sensitivity 0.7800 0.5600 0.7000
## Specificity 0.8600 0.7900 0.8700
## Pos Pred Value 0.7358 0.5714 0.7292
## Neg Pred Value 0.8866 0.7822 0.8529
## Prevalence 0.3333 0.3333 0.3333
## Detection Rate 0.2600 0.1867 0.2333
## Detection Prevalence 0.3533 0.3267 0.3200
## Balanced Accuracy 0.8200 0.6750 0.7850
pca1=prcomp(datos[,-4])
cumsum(pca1$sdev/sum(pca1$sdev))
## [1] 0.7633397 0.9723028 1.0000000
biplot(pca1)
McNemar es un test que en este caso obtiene un valor P de 0.6149, es para evaluar cambios de opinion
Continuación de la clase Ícono de la Universidad Nacional, solo con 27 vectores propios de 250 ya se puede obtener el ícono entendible de la UNal Se pueden recuperar y mejorar imágenes distorsionadas o antiguas cuando un algorítmo puede predecir los pixeles faltantes.
Análisis de Correspondencia Tablas de Contingencia
set.seed(123)
n_fem = 60
n_mas = 40
genero = rep(c('F','M'), c(n_fem, n_mas))
acuerdo = c(
sample(c('SI','NO'), n_fem, TRUE, c(0.8, 0.2)),
sample(c('SI','NO'), n_mas, TRUE, c(0.2, 0.8))
)
tbl=table(genero, acuerdo)
Las tablas de contingencia se aplican fijando algunos parámetros y se aplica una prueba llamada chi cuadrado, y depende de las hipótesis planteadas. 1. hipótesis de independencia: Cuando todo es aleatorio (género,opinión) 2. hipótesis de homogeneidad en distribución 3. McNemar
mosaicplot(tbl,cex=2)
assocplot(tbl)
#Consultar cathegorycal data analysis using R para entender la razón de que las barras tienen diferente dirección, consultar utilidad de los gráficos asociación, tamiz, mosaico (Sieve diagram)
boxplot(tbl)
barplot(t(tbl),legend.text = TRUE)
barplot(t(tbl/rowSums(tbl)),legend.text = TRUE)
barplot(t(tbl/colSums(tbl)),legend.text = TRUE)
tbl
## acuerdo
## genero NO SI
## F 12 48
## M 34 6
tbl_prop=prop.table(tbl,margin = 1)
barplot(t(tbl_prop),legend.text = TRUE)
library(WriteXLS)
library(writexl)
## Warning: package 'writexl' was built under R version 4.2.3
datos = data.frame(genero, acuerdo)
#writexl::write_xlsx(datos, 'datos_052523.xlsx')
Diferewncia entre lo que se observa y lo que se espera 80n Hay 50% probabilidad supuesta que aparezca cada uno E=nx 80x0.5=40 Observé 70 hembras 10 varones Residuales de Pearson entre lo que se observa y lo que se espera
\[\chi^2=\sum\left(\frac{(0_i-E_i)^2}{E_i}\right)\]
Cuando observado y esperado difieren mucho indica que las probabilidades esperadas no son tan acertadas y se recomienda revisarse.
chisq.test(tbl)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tbl
## X-squared = 38.246, df = 1, p-value = 6.235e-10
#Retirando la corrección de Yates
ct=chisq.test(tbl,correct = FALSE)
ct$p.value
## [1] 1.668088e-10
ifelse(ct$p.value<0.05,'Rechaza Ho','No Rechaza Ho')
## [1] "Rechaza Ho"
#PRUEBA CHI^2