
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.1 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
# Crear un data frame con los datos
datos <- data.frame(
Genero = c("Hombres", "Mujeres"),
Frecuencia = c(1524, 1638)
)
# Calcular los porcentajes
datos$Porcentaje <- datos$Frecuencia / sum(datos$Frecuencia) * 100
# Crear el gráfico de pastel
ggplot(datos, aes(x = "", y = Frecuencia, fill = Genero)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 0) +
geom_text(aes(label = paste0(round(Porcentaje), "%")), position = position_stack(vjust = 0.5)) +
labs(title = "Distribución de género", fill = "Género", x = NULL, y = NULL) +
theme_void()

# Crear el data frame
datos <- data.frame(
Nivel_Educativo = c("Analfabeta", "Primaria", "Sec", "Uni", "Doc"),
Numero_Personas = c(160, 1029, 1279, 654, 40)
)
# Factorizar la variable Nivel_Educativo
datos$Nivel_Educativo <- factor(datos$Nivel_Educativo, levels = c("Analfabeta", "Primaria", "Sec", "Uni", "Doc"))
# Realizar la prueba de chi-cuadrado
prueba_chi_cuadrado <- chisq.test(datos$Numero_Personas, p = rep(1/5, 5))
# Imprimir el resultado de la prueba de chi-cuadrado
prueba_chi_cuadrado
##
## Chi-squared test for given probabilities
##
## data: datos$Numero_Personas
## X-squared = 1818.4, df = 4, p-value < 2.2e-16
# Crear el data frame
datos <- data.frame(
Nivel_Educativo = c("Analfabeta", "Primaria", "Sec", "Uni", "Doc"),
Numero_Personas = c(160, 1029, 1279, 654, 40)
)
# Factorizar la variable Nivel_Educativo
datos$Nivel_Educativo <- factor(datos$Nivel_Educativo, levels = c("Analfabeta", "Primaria", "Sec", "Uni", "Doc"))
# Realizar la prueba de chi-cuadrado
prueba_chi_cuadrado <- chisq.test(datos$Numero_Personas, p = rep(1/5, 5))
# Cargar la librería ggplot2
library(ggplot2)
# Calcular los porcentajes
datos$Porcentaje <- datos$Numero_Personas / sum(datos$Numero_Personas) * 100
# Crear el gráfico de barras con el número y porcentaje
ggplot(datos, aes(x = Nivel_Educativo, y = Numero_Personas)) +
geom_bar(stat = "identity", fill = "steelblue") +
geom_text(aes(label = paste(Numero_Personas, " (", round(Porcentaje), "%)", sep = "")),
vjust = -0.5, size = 4) +
annotate("text", x = Inf, y = Inf, label = paste("Chi-cuadrado = ", round(prueba_chi_cuadrado$statistic, 2)),
hjust = 1, vjust = 1, size = 5) +
labs(title = "Personas por nivel educativo",
x = "Nivel educativo",
y = "Número de personas") +
theme_minimal()

# Cargar las librerías necesarias
library(ggplot2)
library(ggmap)
## Warning: package 'ggmap' was built under R version 4.2.3
## ℹ Google's Terms of Service: ]8;;https://mapsplatform.google.com<https://mapsplatform.google.com>]8;;
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
register_google(key = "AIzaSyB0pSKtZ2_SKKa83iudcp-XtgNtqgcSbHQ")
# Obtener el mapa de Sinaloa, México de Google Maps
mapa_sinaloa <- get_map(location = "sinaloa, mexico", zoom = 7)
## ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=sinaloa,%20mexico&zoom=7&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx-XtgNtqgcSbHQhttps://maps.googleapis.com/maps/api/staticmap?center=sinaloa,%20mexico&zoom=7&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx-XtgNtqgcSbHQ]8;;>
## ℹ <]8;;https://maps.googleapis.com/maps/api/geocode/json?address=sinaloa,+mexico&key=xxx-XtgNtqgcSbHQhttps://maps.googleapis.com/maps/api/geocode/json?address=sinaloa,+mexico&key=xxx-XtgNtqgcSbHQ]8;;>
# Crear el data frame con las frecuencias
frecuencias <- data.frame(
Ciudad = c("Culiacan", "Mazatlan", "Mochis"),
Frecuencia = c(1154, 1022, 986),
Latitud = c(24.7998, 23.2494, 25.7933),
Longitud = c(-107.3899, -106.4111, -108.9967)
)
# Crear el mapa con las frecuencias
ggmap(mapa_sinaloa) +
geom_point(data = frecuencias, aes(x = Longitud, y = Latitud, size = Frecuencia, color = Ciudad), alpha = 0.4) +
scale_color_manual(values = c("red", "blue", "yellow")) +
scale_size(range = c(5, 20)) +
labs(title = "Frecuencias de ciudades en Sinaloa, México",
subtitle = "Culiacan, Mazatlan, Mochis",
x = "Longitud",
y = "Latitud") +
theme_void()

# Crear el data frame
datos <- data.frame(
Ciudad = c("Culiacan", "Mazatlan", "Mochis"),
Frecuencia = c(1154, 1022, 986)
)
# Calcular los porcentajes
datos$Porcentaje <- datos$Frecuencia / sum(datos$Frecuencia) * 100
# Crear el gráfico de barras con el número y porcentaje
ggplot(datos, aes(x = Ciudad, y = Frecuencia)) +
geom_bar(stat = "identity", fill = "steelblue") +
geom_text(aes(label = paste(Frecuencia, " (", round(Porcentaje), "%)", sep = "")),
vjust = -0.5, size = 4) +
labs(title = "Frecuencias de ciudades en Sinaloa, México",
x = "Ciudad",
y = "Frecuencia") +
theme_minimal()

# Crear un data frame con los datos
datos <- data.frame(
Estado = c("Consumen", "No consumen"),
Frecuencia = c(654, 2508)
)
# Calcular los porcentajes
datos$Porcentaje <- datos$Frecuencia / sum(datos$Frecuencia) * 100
# Crear el gráfico de barras
ggplot(datos, aes(x = Estado, y = Frecuencia, fill = Estado)) +
geom_bar(stat = "identity", width = 0.5) +
geom_text(aes(label = paste0(round(Porcentaje), "%")),
position = position_dodge(width = 0.5),
vjust = -0.5) +
scale_y_continuous(limits = c(0, max(datos$Frecuencia) * 1.2),
expand = c(0, 0.05)) +
labs(title = "Hábitos de consumo de drogas",
x = "Estado de consumo",
y = "Frecuencia",
fill = NULL) +
theme_classic()

# Crear un data frame con los datos
datos <- data.frame(
Uso = c("Recreacional", "Medicinal"),
Frecuencia = c(486, 168)
)
# Calcular los porcentajes
datos$Porcentaje <- datos$Frecuencia / sum(datos$Frecuencia) * 100
# Crear el gráfico de barras
ggplot(datos, aes(x = Uso, y = Frecuencia, fill = Uso)) +
geom_bar(stat = "identity", width = 0.5) +
geom_text(aes(label = paste0(round(Porcentaje), "%")),
position = position_dodge(width = 0.5),
vjust = -0.5) +
scale_y_continuous(limits = c(0, max(datos$Frecuencia) * 1.2),
expand = c(0, 0.05)) +
labs(title = "Uso de la marihuana",
x = "Tipo de uso",
y = "Frecuencia",
fill = NULL) +
theme_classic()

Por scolaridad
library(gtsummary)
## Warning: package 'gtsummary' was built under R version 4.2.3
library(readxl)
## Warning: package 'readxl' was built under R version 4.2.3
escolaridad <- read_excel("C:/Users/fidel/OneDrive - UNIVERSIDAD AUTONOMA DE SINALOA/COLABS/KOUSUKE/escolaridad.xlsx")
#escolaridad
#glimpse(escolaridad)
#c(`Analfabeta (A)`, `Analfabeta (B)`, `Primaria (A)`, `Primaria (B)`, `Sec (A)`, `Sec (B)`, `Uni (A)`, `Uni (B)`, `Doc (A)`, `Doc (B)`)
#data_long2 <- escolaridad %>% # Apply pivot_longer function
# pivot_longer(c(`Analfabeta_A`, `Analfabeta_B`, `Primaria_A`, `Primaria_B`, `Sec_A`, `Sec_B`, `Uni_A`, `Uni_B`, `Doc_A`, `Doc_B`))
#dbescolaridad <- data_long2 %>% mutate(name= factor(name, levels=c("Analfabeta (A)", "Analfabeta (B)", "Primaria (A)", "Primaria (B)", "Sec (A)", "Sec (B)", "Uni (A)", "Uni (B)", "Doc (A)", "Doc (B)")))
#
#dbescolaridad <- dbescolaridad %>% mutate(value= recode(value, `1`="Mala",
#`2`="Más mala",
#`3`="Nini",
#`4`="Mas buena",
#`5`="Buena")) %>%
# mutate(value = factor(value, levels=c("Buena","Mas buena","Nini","Más mala", "Mala")))
# Convertir el data frame a formato largo para facilitar la representación gráfica
data_long <- escolaridad %>%
mutate(id = 1:n()) %>% # Apply pivot_longer function
pivot_longer(c(`Analfabeta_A`, `Analfabeta_B`, `Primaria_A`, `Primaria_B`, `Sec_A`, `Sec_B`, `Uni_A`, `Uni_B`, `Doc_A`, `Doc_B`)) %>%
separate(col = "name", into = c("Escolaridad", "Intervencion"), sep = "_") %>%
mutate(
Escolaridad = factor(Escolaridad, levels = c("Analfabeta", "Primaria", "Sec", "Uni", "Doc")),
Intervencion = ifelse(Intervencion == "A", "Sin Intervencion", "Con Intervencion")
)
data_long <- data_long %>% mutate(value= recode(value, `1`="Mala",
`2`="Mas mala",
`3`="Nini",
`4`="Mas buena",
`5`="Buena")) %>%
mutate(value = factor(value, levels=c("Mala","Mas mala","Nini","Mas buena","Buena"))) %>%
mutate(Intervencion=factor(Intervencion, levels=c( "Sin Intervencion", "Con Intervencion")))
# Calcular proporciones por escolaridad y ocasión
data_agg <- data_long %>%
group_by(Escolaridad, Intervencion, value) %>%
summarise(n = n()) %>%
mutate(proporcion = n / sum(n))
## `summarise()` has grouped output by 'Escolaridad', 'Intervencion'. You can
## override using the `.groups` argument.
# Crear gráfico de barras apiladas
ggplot(data_agg, aes(x = interaction(Escolaridad), y = proporcion, fill = Intervencion)) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "Escolaridad (Ocasión)", y = "Proporción", fill = "Aceptación",
title = "Comparación de la aceptación del uso de cannabis por escolaridad y ocasión") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

data_agg<-data_agg %>% drop_na()
# Crear el gráfico
ggplot(data_agg, aes(x = Escolaridad, y = proporcion, fill = Intervencion)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~value, nrow = 1, ncol = 5) +
labs(title = "Comparación de aceptación del uso de cannabis por escolaridad e intervención",
x = "Escolaridad",
y = "Frecuencia",
fill = "Intervención") +
theme_minimal()+theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Convertir el data frame a formato largo para facilitar la representación gráfica
data_long <- escolaridad %>%
mutate(id = 1:n()) %>% # Apply pivot_longer function
pivot_longer(c(`Analfabeta_A`, `Analfabeta_B`, `Primaria_A`, `Primaria_B`, `Sec_A`, `Sec_B`, `Uni_A`, `Uni_B`, `Doc_A`, `Doc_B`)) %>%
separate(col = "name", into = c("Escolaridad", "Intervencion"), sep = "_") %>%
mutate(
Escolaridad = factor(Escolaridad, levels = c("Analfabeta", "Primaria", "Sec", "Uni", "Doc")),
Intervencion = ifelse(Intervencion == "A", "Sin Intervencion", "Con Intervencion")
)
data_long <- data_long %>% mutate(value= recode(value, `1`="Mala",
`2`="Mas mala",
`3`="Nini",
`4`="Mas buena",
`5`="Buena")) %>%
mutate(value = factor(value, levels=c("Mala","Mas mala","Nini","Mas buena","Buena"))) %>%
mutate(Intervencion=factor(Intervencion, levels=c( "Sin Intervencion", "Con Intervencion")))
data_long <- data_long %>%
rename(aceptacion = value)
# Calcular proporciones por escolaridad y ocasión
data_agg <- data_long %>%
group_by(Escolaridad, Intervencion, aceptacion) %>%
summarise(n = n()) %>%
mutate(proporcion = n / sum(n))
## `summarise()` has grouped output by 'Escolaridad', 'Intervencion'. You can
## override using the `.groups` argument.
# Crear gráfico de barras apiladas
ggplot(data_agg, aes(x = interaction(Escolaridad), y = proporcion, fill = Intervencion)) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "Escolaridad (Ocasión)", y = "Proporción", fill = "Aceptación",
title = "Comparación de la aceptación del uso de cannabis por escolaridad y ocasión") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

data_agg<-data_agg %>% drop_na()
# Crear el gráfico
ggplot(data_agg, aes(x = Escolaridad, y = proporcion, fill = Intervencion)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~aceptacion, nrow = 1, ncol = 5) +
labs(title = "Comparación de aceptación del uso de cannabis por escolaridad e intervención",
x = "Escolaridad",
y = "Frecuencia",
fill = "Intervención") +
theme_minimal()+theme(axis.text.x = element_text(angle = 45, hjust = 1))

# opcion 2
ggplot(data_agg, aes(x = aceptacion, y = proporcion, fill = Intervencion)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~Escolaridad, nrow = 1, ncol = 5) +
labs(title = "Comparación de aceptación del uso de cannabis por escolaridad e intervención",
x = "Escolaridad",
y = "Frecuencia",
fill = "Intervención") +
theme_minimal()+theme(axis.text.x = element_text(angle = 45,hjust = 1))

Prueba Estadística McNemar por Escolaridad (se representa en
tabla)
# Adaptar el dataframe largo a la estructura que espera la función
#data_estructura <- data_long %>%
# spread(key = "Intervencion", value = "aceptacion") %>%
#rename(aceptacion_A = `Sin Intervencion`, aceptacion_B = `Con Intervencion`)
#data_estructura<-data_estructura %>% drop_na()
# Instalar y cargar el paquete 'coin'
#if (!requireNamespace("coin", quietly = TRUE)) {
# install.packages("coin")
#}
#library(coin)
# Crear una función para aplicar la prueba de Stuart-Maxwell por nivel de escolaridad
#stuart_maxwell_test_por_escolaridad <- function(data, escolaridad) {
# datos_escolaridad <- data[data$escolaridad == escolaridad, ]
# Crear la tabla de contingencia
#tabla_contingencia <- table(datos_escolaridad$aceptacion_A, datos_escolaridad$aceptacion_B)
# dimnames(tabla_contingencia) = list(`Sin Intervencion` = c("Mala", "Mas mala", "Nini", "Mas buena", "Buena"), `Con Intervencion` = c("Mala", "Mas mala", "Nini", "Mas buena", "Buena"))
#tabla_contingencia<-tabla_contingencia %>% addmargins
# Realizar la prueba de Stuart-Maxwell
#test_result <- mh_test(tabla_contingencia)
# Devolver el resultado en un dataframe
# print(test_result
# )
#}
# Aplicar la función a cada nivel de escolaridad y combinar los resultados en una tabla
#niveles_escolaridad <- unique(data$escolaridad)
#resultados_stuart_maxwell <- lapply(niveles_escolaridad, stuart_maxwell_test_por_escolaridad, data = data)
# Mostrar los resultados
#resultados_stuart_maxwell
data_longtb<-data_long %>% mutate(factor(Intervencion, levels=c( "Sin Intervencion", "Con Intervencion")))
data_longtb %>% select(Escolaridad,Intervencion,aceptacion) %>%
tbl_strata(
strata = Intervencion,
.tbl_fun =
~ .x %>%
tbl_summary(by = Escolaridad, percent = "column"))
| Characteristic |
Sin Intervencion
|
Con Intervencion
|
| Analfabeta, N = 1,279 |
Primaria, N = 1,279 |
Sec, N = 1,279 |
Uni, N = 1,279 |
Doc, N = 1,279 |
Analfabeta, N = 1,279 |
Primaria, N = 1,279 |
Sec, N = 1,279 |
Uni, N = 1,279 |
Doc, N = 1,279 |
| aceptacion |
|
|
|
|
|
|
|
|
|
|
| Mala |
91 (57%) |
481 (47%) |
365 (29%) |
98 (15%) |
0 (0%) |
73 (46%) |
409 (40%) |
327 (26%) |
31 (4.7%) |
0 (0%) |
| Mas mala |
49 (31%) |
239 (23%) |
208 (16%) |
24 (3.7%) |
0 (0%) |
32 (20%) |
192 (19%) |
166 (13%) |
11 (1.7%) |
0 (0%) |
| Nini |
5 (3.1%) |
182 (18%) |
194 (15%) |
37 (5.7%) |
0 (0%) |
6 (3.8%) |
32 (3.1%) |
27 (2.1%) |
9 (1.4%) |
0 (0%) |
| Mas buena |
3 (1.9%) |
91 (8.8%) |
309 (24%) |
283 (43%) |
11 (28%) |
21 (13%) |
193 (19%) |
413 (32%) |
287 (44%) |
4 (10%) |
| Buena |
12 (7.5%) |
36 (3.5%) |
203 (16%) |
212 (32%) |
29 (72%) |
28 (18%) |
203 (20%) |
346 (27%) |
316 (48%) |
36 (90%) |
| Unknown |
1,119 |
250 |
0 |
625 |
1,239 |
1,119 |
250 |
0 |
625 |
1,239 |
# Por grupo de edad
library(readxl)
gruposedad <- read_excel("C:/Users/fidel/OneDrive - UNIVERSIDAD AUTONOMA DE SINALOA/COLABS/KOUSUKE/gruposedad.xlsx")
gruposedad
## # A tibble: 830 × 10
## `18-25_A` `18-25_B` `26-35_A` `26-35_B` `36-45_A` `36-45_B` `46-55_A`
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 5 5 5 5 5 5 5
## 2 5 5 5 5 5 5 5
## 3 5 5 5 5 5 5 5
## 4 5 5 5 5 5 5 5
## 5 5 5 5 5 5 5 5
## 6 5 5 5 5 5 5 5
## 7 5 5 5 5 5 5 5
## 8 5 5 5 5 5 5 5
## 9 5 5 5 5 5 5 5
## 10 5 5 5 5 5 5 5
## # ℹ 820 more rows
## # ℹ 3 more variables: `46-55_B` <dbl>, `56-65_A` <dbl>, `56-65_B` <dbl>
data_long <- gruposedad %>%
mutate(id = 1:n()) %>% # Apply pivot_longer function
pivot_longer(c(`18-25_A`, `18-25_B`, `26-35_A`, `26-35_B`, `36-45_A`, `36-45_B`, `46-55_A`, `46-55_B`, `56-65_A`, `56-65_B`)) %>%
separate(col = "name", into = c("Grupos de edad", "Intervencion"), sep = "_") %>%
mutate(Intervencion = ifelse(Intervencion == "A", "Sin Intervencion", "Con Intervencion"))
data_long <- data_long %>% mutate(value= recode(value, `1`="Mala",
`2`="Mas mala",
`3`="Nini",
`4`="Mas buena",
`5`="Buena")) %>%
mutate(value = factor(value, levels=c("Mala","Mas mala","Nini","Mas buena","Buena"))) %>%
mutate(Intervencion=factor(Intervencion, levels=c( "Sin Intervencion", "Con Intervencion")))
# Calcular proporciones por escolaridad y ocasión
data_agg <- data_long %>%
group_by(`Grupos de edad`, Intervencion, value) %>%
summarise(n = n()) %>%
mutate(proporcion = n / sum(n))
## `summarise()` has grouped output by 'Grupos de edad', 'Intervencion'. You can
## override using the `.groups` argument.
# Crear gráfico de barras apiladas
ggplot(data_agg, aes(x = interaction(`Grupos de edad`), y = proporcion, fill = Intervencion)) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "Escolaridad (Ocasión)", y = "Proporción", fill = "Aceptación",
title = "Comparación de la aceptación del uso de cannabis por escolaridad y ocasión") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

data_agg<-data_agg %>% drop_na()
# Crear el gráfico por aceptación
ggplot(data_agg, aes(x = `Grupos de edad`, y = proporcion, fill = Intervencion)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~value, nrow = 1, ncol = 5) +
labs(title = "Comparación de aceptación del uso de cannabis por escolaridad e intervención",
x = "Escolaridad",
y = "Frecuencia",
fill = "Intervención") +
theme_minimal()+theme(axis.text.x = element_text(angle = 45, hjust = 1))

#por grupos de edad
ggplot(data_agg, aes(x =value , y = proporcion, fill = Intervencion)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~`Grupos de edad`, nrow = 1, ncol = 5) +
labs(title = "Comparación de aceptación del uso de cannabis por escolaridad e intervención",
x = "Escolaridad",
y = "Frecuencia",
fill = "Intervención") +
theme_minimal()+theme(axis.text.x = element_text(angle = 45, hjust = 1))
