library(rio)
data=import("BD_LimaComoVamos2021.sav")
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Loading required package: ggplot2
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, units
library(ggplot2)
library(DescTools)
##
## Attaching package: 'DescTools'
## The following objects are masked from 'package:Hmisc':
##
## %nin%, Label, Mean, Quantile
library(e1071)
##
## Attaching package: 'e1071'
## The following object is masked from 'package:Hmisc':
##
## impute
table(data$P6)
##
## 1 2 4 5 8 10 15 20 25 30 35 40 45 49 50 60 70 75 80 90
## 1 2 1 8 1 32 37 48 12 96 3 58 19 1 7 140 1 1 5 56
## 96 97 98 120 140 150 180 210
## 117 364 19 66 1 9 11 1
class(data$P6)
## [1] "numeric"
table(data$sex)
##
## 1 2
## 554 563
class(data$sex)
## [1] "numeric"
data$sex = as.factor(data$sex)
levels(data$sex) = c("hombre" , "mujer")
table(data$sex)
##
## hombre mujer
## 554 563
##Establezco mi hipótesis Definición de hipótesis H0: No hay diferencia estadísticamente significativa entre las media de “sexo” y “tiempo en traslado en minutos” H1: Sí existen diferencias estadísticamente significativas entre las media de “sexo” y “tiempo en traslado en minutos” Calculo las medias
mean(data$P6[data$sex=="mujer"], na.rm=T)
## [1] 77.4476
mean(data$P6[data$sex=="hombre"], na.rm=T)
## [1] 70.64079
#mujer: 77.4476
#hombre 70.64079
#Veamos si esta diferencia de medias se puede extrapolar
t.test(data$P6 ~ data$sex)
##
## Welch Two Sample t-test
##
## data: data$P6 by data$sex
## t = -3.2119, df = 1109.2, p-value = 0.001357
## alternative hypothesis: true difference in means between group hombre and group mujer is not equal to 0
## 95 percent confidence interval:
## -10.964990 -2.648626
## sample estimates:
## mean in group hombre mean in group mujer
## 70.64079 77.44760
#Como se observa, mi p-value es: 0.001357 así que es menor a 0.05 or lo que compruebo mi H1 y sostengo que existe diferencia estadísticamente significativa
library(gplots)
## Registered S3 method overwritten by 'gplots':
## method from
## reorder.factor DescTools
##
## Attaching package: 'gplots'
## The following object is masked from 'package:DescTools':
##
## reorder.factor
## The following object is masked from 'package:stats':
##
## lowess
plotmeans(data$P6 ~ data$sex, data,
mean.labels = TRUE,
digits=2,
connect = FALSE)
class(data$DC2)
## [1] "numeric"
cor.test(data$P6, data$DC2, method = "pearson")
##
## Pearson's product-moment correlation
##
## data: data$P6 and data$DC2
## t = 4.5007, df = 1115, p-value = 7.483e-06
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.07551413 0.19073848
## sample estimates:
## cor
## 0.1335777
ggplot(data, aes(x=DC2, y=P6)) +
geom_point(colour="red") +
xlab("Edad del encuestado") +
ylab("Tiempo de traslado de casa al trabajo") +
theme_light()+ geom_smooth(method="lm", se = F)
## `geom_smooth()` using formula 'y ~ x'
class(data$P5_V2)
## [1] "numeric"
table(data$P5_V2)
##
## 1 2 3 4 5
## 201 74 569 85 171
data$P5_V2 = as.ordered(data$P5_V2)
levels(data$P5_V2) = c("Caminando o a pie","Biclicleta","Transporte público","Taxi","Vehículo privado")
table(data$P5_V2)
##
## Caminando o a pie Biclicleta Transporte público Taxi
## 201 74 569 85
## Vehículo privado
## 171
anova1 <- aov(data$P6 ~ data$P5_V2)
summary(anova1)
## Df Sum Sq Mean Sq F value Pr(>F)
## data$P5_V2 4 24729 6182 4.921 0.000619 ***
## Residuals 1095 1375711 1256
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 17 observations deleted due to missingness
TukeyHSD(anova1)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = data$P6 ~ data$P5_V2)
##
## $`data$P5_V2`
## diff lwr upr
## Biclicleta-Caminando o a pie -15.352225 -28.5208901 -2.1835606
## Transporte público-Caminando o a pie -4.282568 -12.2291519 3.6640163
## Taxi-Caminando o a pie -9.535850 -22.0662465 2.9945462
## Vehículo privado-Caminando o a pie -12.958279 -23.0337338 -2.8828238
## Transporte público-Biclicleta 11.069658 -0.8983754 23.0376905
## Taxi-Biclicleta 5.816375 -9.5815612 21.2143116
## Vehículo privado-Biclicleta 2.393947 -11.0819730 15.8698661
## Taxi-Transporte público -5.253282 -16.5152055 6.0086408
## Vehículo privado-Transporte público -8.675711 -17.1217086 -0.2297134
## Vehículo privado-Taxi -3.422429 -16.2753469 9.4304897
## p adj
## Biclicleta-Caminando o a pie 0.0128961
## Transporte público-Caminando o a pie 0.5806437
## Taxi-Caminando o a pie 0.2297422
## Vehículo privado-Caminando o a pie 0.0041858
## Transporte público-Biclicleta 0.0853927
## Taxi-Biclicleta 0.8404935
## Vehículo privado-Biclicleta 0.9887065
## Taxi-Transporte público 0.7069840
## Vehículo privado-Transporte público 0.0406839
## Vehículo privado-Taxi 0.9501241
plot(TukeyHSD(anova1))
class(data$NSEGRUP)
## [1] "numeric"
table(data$NSEGRUP)
##
## 1 2 3
## 349 550 218
data$NSEGRUP = as.ordered(data$NSEGRUP)
levels(data$NSEGRUP) = c("A/B","C","D/E")
table(data$NSEGRUP)
##
## A/B C D/E
## 349 550 218
anova2 <- aov(data$P6 ~ data$NSEGRUP)
summary(anova2)
## Df Sum Sq Mean Sq F value Pr(>F)
## data$NSEGRUP 2 14430 7215 5.759 0.00325 **
## Residuals 1114 1395546 1253
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(anova2)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = data$P6 ~ data$NSEGRUP)
##
## $`data$NSEGRUP`
## diff lwr upr p adj
## C-A/B -0.6362751 -6.320869 5.048318 0.9626785
## D/E-A/B 8.6523488 1.481605 15.823092 0.0130725
## D/E-C 9.2886239 2.640721 15.936526 0.0030801
plot(TukeyHSD(anova2))
cor.test(data$P6, data$DC2, method = "pearson")
##
## Pearson's product-moment correlation
##
## data: data$P6 and data$DC2
## t = 4.5007, df = 1115, p-value = 7.483e-06
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.07551413 0.19073848
## sample estimates:
## cor
## 0.1335777
TABLA ANOVA Hipótesis para la prueba F:
H0: El modelo de regresión no es válido
H1: El modelo de regresión es válido (variable X aporta al modelo)
modelo1=lm(P6~DC2,data=data)
summary(modelo1)
##
## Call:
## lm(formula = P6 ~ DC2, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -75.82 -32.73 15.14 24.27 140.74
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 61.39696 3.00709 20.417 < 2e-16 ***
## DC2 0.31473 0.06993 4.501 7.48e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 35.24 on 1115 degrees of freedom
## Multiple R-squared: 0.01784, Adjusted R-squared: 0.01696
## F-statistic: 20.26 on 1 and 1115 DF, p-value: 7.483e-06
modelo1$coefficients
## (Intercept) DC2
## 61.3969590 0.3147322
summary(data$DC2)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 18.00 28.00 38.00 40.27 51.00 88.00
(Intercept) DC2 61.3969590 0.3147322
ggplot(data, aes(x=DC2, y=P6)) +
geom_point(colour="red") +
xlab("Edad del encuestado") +
ylab("Tiempo de traslado de casa al trabajo") +
theme_light()+ geom_smooth(method="lm", se = F)
## `geom_smooth()` using formula 'y ~ x'
plot(data$P6,data$PC2)
abline(lm(data$P6~data$DC2))
5. Finalmente, los investigadores quieren evaluar qué afecta a los
aspectos de la calidad de vida en Lima. Primero elaboran un índice sobre
la percepción de la calidad de vida en Lima Metropolitana (Lima y
Callao). Este indicador va de 0 a 100, con 0 que significa “Nada de
satisfacción con la calidad de vida en Lima Metropolitana” y 100 que
significa “Mucha satisfacción con la calidad de vida en lima
Metropolitana”. Las variables que se emplean para la construcción del
índice son: P3_1, P3_2 ,P3_3, P3_4 ,P3_5, P3_6, P3_7 y P3_8. (No es
necesario realizar la prueba de normalidad)
data$calidad= (((data$P3_1 + data$P3_2 + data$P3_3+ data$P3_4 + data$P3_5+ data$P3_6+ data$P3_7 + data$P3_8)-8)/32*100)
summary(data$calidad)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 0.00 18.75 34.38 33.64 46.88 100.00 54
hist(data$calidad)
Var(data$calidad,na.rm = TRUE)
## [1] 333.4081
sd(data$calidad,na.rm = TRUE)
## [1] 18.25947
skewness(data$calidad,na.rm = TRUE)
## [1] 0.1925778
kurtosis(data$calidad,na.rm = TRUE)
## [1] -0.3340478
Mode(data$calidad,na.rm = TRUE)
## [1] 40.625
## attr(,"freq")
## [1] 70
boxplot(data$calidad)
Realice un modelo de regresión lineal múltiple considerando como
variable dependiente el indicador de percepción sobre la calidad de vida
en Lima Metropolitana. Incluya como variables independientes:
class(data$P1)
## [1] "numeric"
summary(data$P1)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 1.000 1.000 1.367 2.000 2.000
table(data$P1)
##
## 1 2
## 707 410
##mujer
data$mujer=factor(ifelse(data$sex=="mujer",1,0))
table(data$mujer)
##
## 0 1
## 554 563
##lima
class(data$DC3)
## [1] "numeric"
data$DC3 = as.factor(data$DC3)
levels(data$DC3) = c("Lima" , "Callao")
table(data$DC3)
##
## Lima Callao
## 893 224
data$lima=factor(ifelse(data$DC3=="Lima",1,0))
##Dummy de NSE C
table(data$NSEGRUP)
##
## A/B C D/E
## 349 550 218
data$NSEC=factor(ifelse(data$NSEGRUP=="C",1,0))
table(data$NSEC)
##
## 0 1
## 567 550
## Dummy de NSE D/E
data$NSEDE=factor(ifelse(data$NSEGRUP=="D/E",1,0))
table(data$NSEDE)
##
## 0 1
## 899 218
modelo5 <- lm(data$calidad ~ data$P1 + data$mujer + data$DC2 + data$lima + data$P6 + data$NSEC + data$NSEDE)
summary(modelo5)
##
## Call:
## lm(formula = data$calidad ~ data$P1 + data$mujer + data$DC2 +
## data$lima + data$P6 + data$NSEC + data$NSEDE)
##
## Residuals:
## Min 1Q Median 3Q Max
## -39.914 -12.888 0.299 12.168 70.399
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 46.40846 2.63766 17.595 < 2e-16 ***
## data$P1 3.42781 1.17822 2.909 0.003698 **
## data$mujer1 -3.26987 1.08271 -3.020 0.002588 **
## data$DC2 -0.19445 0.03746 -5.191 2.51e-07 ***
## data$lima1 -4.66675 1.38656 -3.366 0.000791 ***
## data$P6 -0.01953 0.01535 -1.272 0.203579
## data$NSEC1 -2.57425 1.24066 -2.075 0.038237 *
## data$NSEDE1 -8.06658 1.57634 -5.117 3.68e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 17.55 on 1055 degrees of freedom
## (54 observations deleted due to missingness)
## Multiple R-squared: 0.08278, Adjusted R-squared: 0.0767
## F-statistic: 13.6 on 7 and 1055 DF, p-value: < 2.2e-16
modelo5$coefficients
## (Intercept) data$P1 data$mujer1 data$DC2 data$lima1 data$P6
## 46.40845712 3.42781296 -3.26986807 -0.19444672 -4.66674845 -0.01952755
## data$NSEC1 data$NSEDE1
## -2.57424783 -8.06657677