Cargar datos y librerias
library(readxl)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(janitor)
##
## Attaching package: 'janitor'
##
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
library(lubridate)
library(ggplot2)
datos_turismo <- read_excel("C:/Users/Asus ZenBook/Downloads/Base de Datos SP Turismo Responsable.xlsx",
sheet = "Datos")
## New names:
## • `` -> `...59`
spain_data <- datos_turismo[datos_turismo$Q13 == 2, ]
mexico_data <- datos_turismo[datos_turismo$Q13 == 1, ]
Hipotesis 1
PH para una proporción. El (86.6%) de los españoles reservan su
viaje por internet ellos mismos
Hipótesis nula: la proporción de turistas españoles que reservan su
viaje ellos mismos es igual a 86.6%.
Hipótesis alterna: la proporción de turistas españoles que reservan
su viaje ellos mismos es diferente a 86.6%.
q9_data <- as.numeric(spain_data$Q9)
prop_booked_own <- sum(q9_data == 1) / length(q9_data)
hypothesized_prop <- 0.866
test_result <- t.test(q9_data, alternative = "two.sided", conf.level = 0.95, mu = hypothesized_prop)
test_result
##
## One Sample t-test
##
## data: q9_data
## t = 22.807, df = 390, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0.866
## 95 percent confidence interval:
## 1.773705 1.944965
## sample estimates:
## mean of x
## 1.859335
cat("P-value:", test_result$p.value, "\n\n")
## P-value: 9.039902e-74
if (test_result$p.value < 0.05) {
cat("Conclusion: rechazar el hypothesis\n")
} else {
cat("Conclusion: no rechazar el hypothesis\n")
}
## Conclusion: rechazar el hypothesis
Gráfica de hipótesis 1
combined_data <- bind_rows(
mutate(spain_data, Country = "Spain"),
mutate(mexico_data, Country = "Mexico")
)
rename_values <- function(data) {
data$Q9 <- case_when(
data$Q9 == 1 ~ "Principalmente yo",
data$Q9 == 2 ~ "Principalmente otras personas",
data$Q9 == 3 ~ "Tanto yo como otras personas"
)
return(data)
}
combined_data <- rename_values(combined_data)
combined_data <- combined_data %>%
filter(Q9 != "Unknown")
ggplot(combined_data, aes(x = Country, fill = Q9)) +
geom_bar() +
labs(title = "Quien planea los viajes? (Q9) - Spain vs. Mexico",
x = "Country",
y = "Count") +
theme_minimal() +
scale_fill_manual(values = c(
"Principalmente yo" = "skyblue",
"Principalmente otras personas" = "salmon",
"Tanto yo como otras personas" = "green"
)) +
coord_flip()

Hipotesis 2
PH para una proporción. Casi la mitad de los viajeros en México
eligen un destino con la intención de contribuir en la economía de una
comunidad local
Hipótesis nula: la proporción de turistas mexicanos que fomento el
desarrollo local del destino consumiendo productos y servicios locales
es mayor o igual al 50%.
Hipótesis alterna: la proporción de turistas mexicanos que fomento
el desarrollo local del destino consumiendo productos y servicios
locales es menor al 50%
Q6_4_data <- as.numeric(mexico_data$Q6_4)
prop_agree <- mean(Q6_4_data)
hypothesized_prop_2 <- 0.50
test_result_2 <- t.test(Q6_4_data, alternative = "two.sided", conf.level = 0.95, mu = hypothesized_prop_2)
test_result_2
##
## One Sample t-test
##
## data: Q6_4_data
## t = 61.873, df = 531, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0.5
## 95 percent confidence interval:
## 4.789787 5.071116
## sample estimates:
## mean of x
## 4.930451
cat("P-value:", test_result_2$p.value, "\n\n")
## P-value: 6.528705e-245
if (test_result_2$p.value < 0.5) {
cat("Conclusion: rechazar el hypothesis\n")
} else {
cat("Conclusion: no rechazar el hypothesis\n")
}
## Conclusion: rechazar el hypothesis
Gráfica de hipótesis 2
spain_data$Country <- "Espana"
mexico_data$Country <- "Mexico"
combined_data <- rbind(spain_data, mexico_data)
ggplot(combined_data, aes(x = factor(Q6_4, levels = 1:7, labels = c("No", "Casi no", "Algo", "A veces", "Procuro sí", "Casi siempre", "Siempre")), fill = Country)) +
geom_bar(position = "dodge") +
labs(title = "Q6_4: Consumo de productos y servicios locales.",
x = "Categories",
y = "Count") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

Hipotesis 3
PHp para una proporción. El 55% de los encuestados españoles afirman
que, si tuvieran la posibilidad, compensarían la huella de carbono en su
alojamiento vacacional.
Hipótesis nula: la proporción de turistas españoles que afirman que
compensarían la huella de carbono en su alojamiento vacacional es igual
al 55%.
Hipótesis alterna: la proporción de turistas españoles que afirman
que compensarían la huella de carbono en su alojamiento vacacional es
diferente al 55%.
q611_data <- as.numeric(spain_data$Q6_11)
prop_huella_carb <- mean(q611_data)
hypothesized_prop_3 <- 0.55
test_result_3 <- t.test(q611_data, alternative = "two.sided", conf.level = 0.95, mu = hypothesized_prop_3)
test_result_3
##
## One Sample t-test
##
## data: q611_data
## t = 21.395, df = 400, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0.55
## 95 percent confidence interval:
## 2.346865 2.710492
## sample estimates:
## mean of x
## 2.528678
cat("P-value:", test_result_3$p.value, "\n\n")
## P-value: 2.993515e-68
if (test_result_3$p.value < 0.05) {
cat("Conclusion: rechazar el hypothesis\n")
} else {
cat("Conclusion: no rechazar el hypothesis\n")
}
## Conclusion: rechazar el hypothesis
Gráfica 1 de hipótesis 4
En esta gráfica también se analizan otros variables
selected_columns <- datos_turismo %>%
select(Q6_1:Q6_18)
counts <- selected_columns %>%
summarise_all(~ sum(. == 1, na.rm = TRUE))
counts_long <- counts %>%
pivot_longer(cols = everything(), names_to = "Question", values_to = "Count")
ggplot(counts_long, aes(x = Question, y = Count)) +
geom_bar(stat = "identity", fill = "skyblue") +
geom_text(aes(label = Count), vjust = -0.5, size = 3) +
labs(title = "Veces que se contestó Sí: TOTAL",
x = "Questions",
y = "Count") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

Gráfica 2 de hipótesis 4
En esta gráfica también se analizan otros variables
selected_columns <- spain_data %>%
select(Q6_1:Q6_18)
counts <- selected_columns %>%
summarise_all(~ sum(. == 1, na.rm = TRUE))
counts_long <- counts %>%
pivot_longer(cols = everything(), names_to = "Question", values_to = "Count")
ggplot(counts_long, aes(x = Question, y = Count)) +
geom_bar(stat = "identity", fill = "darkred") +
geom_text(aes(label = Count), vjust = -0.5, size = 3) +
labs(title = "Veces que se contestó Sí: ESPAÑA",
x = "Questions",
y = "Count") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

Gráfica 3 de hipótesis 4
En esta gráfica también se analizan otros variables
selected_columns <- mexico_data %>%
select(Q6_1:Q6_18)
counts <- selected_columns %>%
summarise_all(~ sum(. == 1, na.rm = TRUE))
counts_long <- counts %>%
pivot_longer(cols = everything(), names_to = "Question", values_to = "Count")
ggplot(counts_long, aes(x = Question, y = Count)) +
geom_bar(stat = "identity", fill = "darkgreen") +
geom_text(aes(label = Count), vjust = -0.5, size = 3) +
labs(title = "Veces que se contestó Sí: MEXICO",
x = "Questions",
y = "Count") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

Hipotesis 4
Gráfica de hipótesis 4
ggplot(combined_data, aes(x = Country, y = Q7, fill = Country)) +
geom_point() +
labs(title = "Veces que viajaron en el último año",
x = "País",
y = "Frecuencia de viajes") +
scale_fill_manual(values = c("España" = "blue", "Mexico" = "red")) +
theme_minimal()
## Warning: Removed 16 rows containing missing values (`geom_point()`).

Hipótesis 5
Gráfica de hipótesis 5
ggplot() +
geom_histogram(data = spain_data, aes(x = Q12, fill = "Spain"), alpha = 0.8, bins = 20) +
geom_histogram(data = mexico_data, aes(x = Q12, fill = "Mexico"), alpha = 0.8, bins = 20) +
labs(title = "Distribución de edades entre Espana y Mexico",
x = "Edad",
y = "Frecuencia") +
scale_fill_manual(values = c("Spain" = "blue", "Mexico" = "red")) +
theme_minimal()
## Warning: Removed 3 rows containing non-finite values (`stat_bin()`).
## Removed 3 rows containing non-finite values (`stat_bin()`).

Hipótesis 6
Gráfica de hipótesis 6
spain_data$Country <- "Spain"
mexico_data$Country <- "Mexico"
combined_data <- rbind(spain_data, mexico_data)
ggplot(combined_data, aes(x = Country, y = Q12, fill = Country)) +
geom_boxplot() +
labs(title = "Distribución de edades entre Espana y Mexico",
x = "País",
y = "Edad") +
scale_fill_manual(values = c("Spain" = "blue", "Mexico" = "red")) +
theme_minimal()
## Warning: Removed 6 rows containing non-finite values (`stat_boxplot()`).

Gráfica de cantidad de personas por país
datos_turismo <- datos_turismo %>%
filter(!is.na(Q13))
q13_counts <- datos_turismo %>%
group_by(Q13) %>%
summarize(Count = n())
q13_counts$Q13 <- case_when(
q13_counts$Q13 == 1 ~ "Mexico",
q13_counts$Q13 == 2 ~ "Spain"
)
ggplot(q13_counts, aes(x = Q13, y = Count)) +
geom_bar(stat = "identity", fill = "skyblue") +
geom_text(aes(label = Count), vjust = -0.5, size = 3) +
labs(title = "Cantidad de personas de México vs. Espana (Q13)",
x = "Country",
y = "Count") +
theme_minimal() +
theme(text = element_text(size = 10))

Gráfica de razones para viajar por país
combined_data <- bind_rows(
mutate(spain_data, Country = "Spain"),
mutate(mexico_data, Country = "Mexico")
)
rename_values <- function(data) {
data$Q10 <- case_when(
data$Q10 == 1 ~ "Principalmente ocio o visita a familiares",
data$Q10 == 2 ~ "Principalmente estudio",
data$Q10 == 3 ~ "Principalmente trabajo",
data$Q10 == 4 ~ "Otras razones",
TRUE ~ "Unknown"
)
return(data)
}
combined_data <- rename_values(combined_data)
combined_data <- combined_data %>%
filter(Q10 != "Unknown")
ggplot(combined_data, aes(x = Country, fill = Q10)) +
geom_bar() +
labs(title = "Razones para viajar (Q10) - Spain vs. Mexico",
x = "Country",
y = "Count") +
theme_minimal() +
scale_fill_manual(values = c(
"Principalmente ocio o visita a familiares" = "pink",
"Principalmente estudio" = "skyblue",
"Principalmente trabajo" = "yellow",
"Otras razones" = "purple",
"Unknown" = "gray"
))
