data <- read_csv("C:\\Users\\jmongev.IMAS\\OneDrive - Instituto Mixto de Ayuda Social - IMAS\\Escritorio\\EntornoRStudio\\Ari\\Datos.csv",
locale = locale(encoding = "UTF-8"))
attach(data)
## # A tibble: 6 × 7
## SanJosé Alajuela Cartago Heredia Puntarenas Limón Guanacaste
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 93 55 103 105 73 172 109
## 2 128 64 63 147 140 116 109
## 3 134 88 126 80 123 101 119
## 4 143 96 94 102 113 51 292
## 5 54 135 136 108 70 160 89
## 6 137 83 131 125 165 87 110
# str(datos)
# summary(datos)
data %>%
head(7) %>%
kable() %>%
kable_styling("striped", full_width = FALSE, position = "center", font_size = 16) %>%
row_spec(0, monospace = TRUE, bold = TRUE, color = "navy", background = "grey") %>%
row_spec(1:ncol(data), color = "black", background = "lightgrey")
SanJosé | Alajuela | Cartago | Heredia | Puntarenas | Limón | Guanacaste |
---|---|---|---|---|---|---|
93 | 55 | 103 | 105 | 73 | 172 | 109 |
128 | 64 | 63 | 147 | 140 | 116 | 109 |
134 | 88 | 126 | 80 | 123 | 101 | 119 |
143 | 96 | 94 | 102 | 113 | 51 | 292 |
54 | 135 | 136 | 108 | 70 | 160 | 89 |
137 | 83 | 131 | 125 | 165 | 87 | 110 |
108 | 157 | 107 | 77 | 159 | 103 | 122 |
variables_numericas_df = data %>%
select(where(is.numeric))
estadisticos_numericos_df = data.frame(Variable = character(),
Media = numeric(),
Mediana = numeric(),
IQR = numeric())
for (i in names(variables_numericas_df)) {
media = round(mean(variables_numericas_df[[i]], na.rm = TRUE),2)
mediana = round(median(variables_numericas_df[[i]], na.rm = TRUE),2)
IQR = round(IQR(variables_numericas_df[[i]], na.rm = TRUE),2)
temp_df <- data.frame(Variable = i,
Media = media,
Mediana = mediana,
IQR = IQR)
estadisticos_numericos_df <- rbind(estadisticos_numericos_df, temp_df)
}
# print(estadisticos_numericos_df)
estadisticos_numericos_df %>%
kable() %>%
kable_styling("striped", full_width = FALSE, position = "center", font_size = 16) %>%
row_spec(0, monospace = TRUE, bold = TRUE, color = "navy", background = "grey") %>%
row_spec(1:nrow(estadisticos_numericos_df), color = "black", background = "lightgrey") %>%
column_spec(1, bold = TRUE)
Variable | Media | Mediana | IQR |
---|---|---|---|
SanJosé | 102.14 | 101.5 | 35.50 |
Alajuela | 115.68 | 116.5 | 36.25 |
Cartago | 105.08 | 106.5 | 28.50 |
Heredia | 105.78 | 104.0 | 31.00 |
Puntarenas | 114.16 | 114.0 | 34.75 |
Limón | 114.90 | 115.5 | 32.50 |
Guanacaste | 118.72 | 117.0 | 35.50 |
data_long <- gather(variables_numericas_df, key = "variable", value = "value")
p <- ggplot(data_long, aes(x = variable, y = value, fill = variable)) +
geom_boxplot() +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, hjust =1),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
labs(title = "Boxplot para cada variable númerica",
x = "Variable",
y = "Valor")
p_interactive <- ggplotly(p)
p_interactive
outliers_df <- data.frame()
for (variable in colnames(variables_numericas_df)) {
data <-variables_numericas_df[[variable]]
data <- data[!is.na(data)]
IQR <- IQR(data, na.rm = TRUE)
Q1 <-quantile(data,.25, na.rm = TRUE)
Q3 <-quantile(data,.75, na.rm = TRUE)
lower_bound <- Q1 - 1.5 * IQR
upper_bound <- Q3 + 1.5 * IQR
outliers <- data[data < lower_bound | data > upper_bound]
if(length(outliers)>0){
temp_df <- data.frame(variable = variable, outlier = outliers)
outliers_df <- rbind(outliers_df, temp_df)
}
}
outliers_df
## variable outlier
## 1 Cartago 37
## 2 Cartago 169
## 3 Cartago 46
## 4 Heredia 174
## 5 Guanacaste 292
comprobar_normalidad <-function(data){
resultado <- data.frame()
for (nombre_variable in names(data)) {
if(is.numeric(data[[nombre_variable]])){
test <- shapiro.test(data[[nombre_variable]])
es_normal <- ifelse(test$p.value > 0.05, "Si", "No")
temp_df <- data.frame(variable = nombre_variable, p.value = test$p.value, Distribucion_normal = es_normal)
resultado <- rbind(resultado, temp_df)
}
}
return(resultado)
}
resultado <- comprobar_normalidad(variables_numericas_df)
print(resultado)
## variable p.value Distribucion_normal
## 1 SanJosé 6.350839e-01 Si
## 2 Alajuela 8.647360e-01 Si
## 3 Cartago 3.955757e-01 Si
## 4 Heredia 5.100282e-01 Si
## 5 Puntarenas 3.782597e-01 Si
## 6 Limón 9.850457e-01 Si
## 7 Guanacaste 3.439474e-06 No
# Calcular normalidad para todas las variables numéricas
test_normalidad <- lapply(variables_numericas_df, shapiro.test)
# Crear una tabla resumen con los resultados
resultados_normalidad <- data.frame(
Variable = names(test_normalidad),
W = sapply(test_normalidad, function(x) round(x$statistic, 5)),
p_value = sapply(test_normalidad, function(x) round(x$p.value, 5)),
Normalidad = sapply(test_normalidad, function(x) ifelse(x$p.value > 0.05, "Sí", "No"))
)
# Imprimir la tabla resumen
print("Resultados del test de Shapiro-Wilk para normalidad:")
## [1] "Resultados del test de Shapiro-Wilk para normalidad:"
## Variable W p_value Normalidad
## SanJosé.W SanJosé 0.98192 0.63508 Sí
## Alajuela.W Alajuela 0.98732 0.86474 Sí
## Cartago.W Cartago 0.97593 0.39558 Sí
## Heredia.W Heredia 0.97899 0.51003 Sí
## Puntarenas.W Puntarenas 0.97541 0.37826 Sí
## Limón.W Limón 0.99232 0.98505 Sí
## Guanacaste.W Guanacaste 0.82482 0.00000 No
histograma <- function(df){
columnas_numericas <- sapply(df, is.numeric)
for(nombre_columna in names(df) [columnas_numericas]){
p <- ggplot(df, aes_string(nombre_columna)) +
geom_histogram(binwidth = 10, fill= "darkmagenta", color ="black") +
labs(title= paste("Histograma de", nombre_columna),
x = nombre_columna,
y = "Recurrencia")
print(p)
}
}
data <- read_csv("C:\\Users\\jmongev.IMAS\\OneDrive - Instituto Mixto de Ayuda Social - IMAS\\Escritorio\\EntornoRStudio\\Ari\\Datos.csv",
locale = locale(encoding = "UTF-8"))
histograma(data)