##Importación de los datos:

data<-read.csv("C:\\Users\\DELL\\Downloads\\student-por.csv" ,sep = ";",dec = ".")

##Transformar a factor las variables “character”

library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
df <- data %>%
mutate(across(where(is.character), as.factor))

str(df)
## 'data.frame':    649 obs. of  33 variables:
##  $ school    : Factor w/ 2 levels "GP","MS": 1 1 1 1 1 1 1 1 1 1 ...
##  $ sex       : Factor w/ 2 levels "F","M": 1 1 1 1 1 2 2 1 2 2 ...
##  $ age       : int  18 17 15 15 16 16 16 17 15 15 ...
##  $ address   : Factor w/ 2 levels "R","U": 2 2 2 2 2 2 2 2 2 2 ...
##  $ famsize   : Factor w/ 2 levels "GT3","LE3": 1 1 2 1 1 2 2 1 2 1 ...
##  $ Pstatus   : Factor w/ 2 levels "A","T": 1 2 2 2 2 2 2 1 1 2 ...
##  $ Medu      : int  4 1 1 4 3 4 2 4 3 3 ...
##  $ Fedu      : int  4 1 1 2 3 3 2 4 2 4 ...
##  $ Mjob      : Factor w/ 5 levels "at_home","health",..: 1 1 1 2 3 4 3 3 4 3 ...
##  $ Fjob      : Factor w/ 5 levels "at_home","health",..: 5 3 3 4 3 3 3 5 3 3 ...
##  $ reason    : Factor w/ 4 levels "course","home",..: 1 1 3 2 2 4 2 2 2 2 ...
##  $ guardian  : Factor w/ 3 levels "father","mother",..: 2 1 2 2 1 2 2 2 2 2 ...
##  $ traveltime: int  2 1 1 1 1 1 1 2 1 1 ...
##  $ studytime : int  2 2 2 3 2 2 2 2 2 2 ...
##  $ failures  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ schoolsup : Factor w/ 2 levels "no","yes": 2 1 2 1 1 1 1 2 1 1 ...
##  $ famsup    : Factor w/ 2 levels "no","yes": 1 2 1 2 2 2 1 2 2 2 ...
##  $ paid      : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
##  $ activities: Factor w/ 2 levels "no","yes": 1 1 1 2 1 2 1 1 1 2 ...
##  $ nursery   : Factor w/ 2 levels "no","yes": 2 1 2 2 2 2 2 2 2 2 ...
##  $ higher    : Factor w/ 2 levels "no","yes": 2 2 2 2 2 2 2 2 2 2 ...
##  $ internet  : Factor w/ 2 levels "no","yes": 1 2 2 2 1 2 2 1 2 2 ...
##  $ romantic  : Factor w/ 2 levels "no","yes": 1 1 1 2 1 1 1 1 1 1 ...
##  $ famrel    : int  4 5 4 3 4 5 4 4 4 5 ...
##  $ freetime  : int  3 3 3 2 3 4 4 1 2 5 ...
##  $ goout     : int  4 3 2 2 2 2 4 4 2 1 ...
##  $ Dalc      : int  1 1 2 1 1 1 1 1 1 1 ...
##  $ Walc      : int  1 1 3 1 2 2 1 1 1 1 ...
##  $ health    : int  3 3 3 5 5 5 3 1 1 5 ...
##  $ absences  : int  4 2 6 0 0 6 0 2 0 0 ...
##  $ G1        : int  0 9 12 14 11 12 13 10 15 12 ...
##  $ G2        : int  11 11 13 14 13 12 12 13 16 12 ...
##  $ G3        : int  11 11 12 14 13 13 13 13 17 13 ...

Con str() podemos observar que las variables character se transformaron a factor

##Creación de matriz con la conversion de Clases realizada :

Classes=sapply(df,class)
for (i in 1: ncol(df))
  if (Classes[i]=="integer")
    df[[i]]=as.numeric(df[[i]])
Classes=sapply(df,class)
Classes
##     school        sex        age    address    famsize    Pstatus       Medu 
##   "factor"   "factor"  "numeric"   "factor"   "factor"   "factor"  "numeric" 
##       Fedu       Mjob       Fjob     reason   guardian traveltime  studytime 
##  "numeric"   "factor"   "factor"   "factor"   "factor"  "numeric"  "numeric" 
##   failures  schoolsup     famsup       paid activities    nursery     higher 
##  "numeric"   "factor"   "factor"   "factor"   "factor"   "factor"   "factor" 
##   internet   romantic     famrel   freetime      goout       Dalc       Walc 
##   "factor"   "factor"  "numeric"  "numeric"  "numeric"  "numeric"  "numeric" 
##     health   absences         G1         G2         G3 
##  "numeric"  "numeric"  "numeric"  "numeric"  "numeric"
View(df)

##Detección de datos perdidos (Encontrar NA’S)

which(is.na(df))
## integer(0)

Se obtiene que no hay datos perdidos o faltantes en la base de datos “df (data student transformada)”.

##Realizar el escalamiento de las variables numéricas

df[,Classes=="integer"]= scale(df[,Classes=="integer"])
head(df)

Conversión de las variables Cualitativas categóricas en Variables Ficticias con 10 variables significativas.

##10 datos

dataj<-df[,c(2,3,7,19,20,21,22,23,24,25,26)]
tr = round(nrow(df)*0.7)

set.seed(1805753298) # semilla para el entrenamiento
muestra = sample.int(nrow(dataj), tr) # tomo una muestra del 70%
Train.higher = dataj[muestra,] #tomo los datos para entrenamiento
Val.higher = dataj[-muestra,]  #tomo los datos para validación 

Las varibles categóricas aparecen con 0 y 1.

x=model.matrix(df$health~.,data=dataj)
head(x)
  (Intercept) sexM age Medu activitiesyes nurseryyes higheryes internetyes
1           1    0  18    4             0          1         1           0
2           1    0  17    1             0          0         1           1
3           1    0  15    1             0          1         1           1
4           1    0  15    4             1          1         1           1
5           1    0  16    3             0          1         1           0
6           1    1  16    4             1          1         1           1
  romanticyes famrel freetime goout
1           0      4        3     4
2           0      5        3     3
3           0      4        3     2
4           1      3        2     2
5           0      4        3     2
6           0      5        4     2

Interpretación:muestra información de estudiantes como sexo (sexM), edad (age), educación de la madre (Medu), actividades extracurriculares (activitiesyes), asistencia a guardería (nurseryyes), intención de seguir estudios superiores (higheryes), y acceso a Internet (internetyes). También incluye datos sobre relaciones románticas (romanticyes), relación familiar (famrel), tiempo libre (freetime), y frecuencia de salidas (goout).

Kernel Sigmoidal

library(e1071)
## Warning: package 'e1071' was built under R version 4.2.3
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
## Warning: package 'lattice' was built under R version 4.2.3
fitsvm1<-svm(Train.higher$higher~.,data=Train.higher,kernel= "sigmoid" )
summary(fitsvm1)
## 
## Call:
## svm(formula = Train.higher$higher ~ ., data = Train.higher, kernel = "sigmoid")
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  sigmoid 
##        cost:  1 
##      coef.0:  0 
## 
## Number of Support Vectors:  118
## 
##  ( 57 61 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  no yes
predictedSVM <- predict(fitsvm1, Val.higher)
matrizSVM <- confusionMatrix(Val.higher$higher, predictedSVM)
matrizSVM
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  no yes
##        no    1  11
##        yes   0 183
##                                           
##                Accuracy : 0.9436          
##                  95% CI : (0.9013, 0.9715)
##     No Information Rate : 0.9949          
##     P-Value [Acc > NIR] : 1.000000        
##                                           
##                   Kappa : 0.1458          
##                                           
##  Mcnemar's Test P-Value : 0.002569        
##                                           
##             Sensitivity : 1.000000        
##             Specificity : 0.943299        
##          Pos Pred Value : 0.083333        
##          Neg Pred Value : 1.000000        
##              Prevalence : 0.005128        
##          Detection Rate : 0.005128        
##    Detection Prevalence : 0.061538        
##       Balanced Accuracy : 0.971649        
##                                           
##        'Positive' Class : no              
## 

Interpretación: El modelo de predicción indica que el 94.36% de los estudiantes se espera que continúen sus estudios universitarios, mientras que solo el 0.51% no seguirá estudiando según las predicciones realizadas.

##Random Forest

library(caret)
library(randomForest)
## Warning: package 'randomForest' was built under R version 4.2.3
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:ggplot2':
## 
##     margin
## The following object is masked from 'package:dplyr':
## 
##     combine
fitRF <- randomForest(higher ~., data = Train.higher, ntree = 500)
summary(fitRF)
##                 Length Class  Mode     
## call               4   -none- call     
## type               1   -none- character
## predicted        454   factor numeric  
## err.rate        1500   -none- numeric  
## confusion          6   -none- numeric  
## votes            908   matrix numeric  
## oob.times        454   -none- numeric  
## classes            2   -none- character
## importance        10   -none- numeric  
## importanceSD       0   -none- NULL     
## localImportance    0   -none- NULL     
## proximity          0   -none- NULL     
## ntree              1   -none- numeric  
## mtry               1   -none- numeric  
## forest            14   -none- list     
## y                454   factor numeric  
## test               0   -none- NULL     
## inbag              0   -none- NULL     
## terms              3   terms  call
#Predict Output
predictedRF <- predict(fitRF, Val.higher)
matrizRF <- confusionMatrix(Val.higher$higher, predictedRF)
matrizRF
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  no yes
##        no    1  11
##        yes   3 180
##                                           
##                Accuracy : 0.9282          
##                  95% CI : (0.8825, 0.9602)
##     No Information Rate : 0.9795          
##     P-Value [Acc > NIR] : 0.99999         
##                                           
##                   Kappa : 0.0972          
##                                           
##  Mcnemar's Test P-Value : 0.06137         
##                                           
##             Sensitivity : 0.250000        
##             Specificity : 0.942408        
##          Pos Pred Value : 0.083333        
##          Neg Pred Value : 0.983607        
##              Prevalence : 0.020513        
##          Detection Rate : 0.005128        
##    Detection Prevalence : 0.061538        
##       Balanced Accuracy : 0.596204        
##                                           
##        'Positive' Class : no              
## 

El modelo Random Forest tiene una precisión del 92.82% en predecir qué estudiantes continuarán estudiando en la universidad específicamente, el modelo identifica correctamente al 25% de los casos que no continuarán (sensibilidad) y al 94.24% de los casos que sí continuarán (especificidad). La tasa de prevalencia de estudiantes que no continuarán es del 2.05%.

###Conclusion:

El modelo SVM con kernel sigmoidal muestra una especificidad más alta (94.33%), lo que significa que identifica bien a los estudiantes que continuarán en la universidad. En contraste, el Random Forest tiene una sensibilidad algo mayor (25%) para detectar a los estudiantes que no continuarán, pero una especificidad ligeramente menor (94.24%). En resumen, el SVM con kernel sigmoidal es mejor en identificar correctamente a los estudiantes que continuarán según estos resultados específicos.