Laboratorio del 6 de Septiembre de 2017
Abrimos la base de datos
setwd("/Users/mau/Dropbox/Cursos mau/Maestria CEI")
data<-read.csv("BaseIP.csv",header=TRUE)
Aqui voy a usar alguna pregunta muy simple de investigacion: P1: ??Cual es la asociacion entre niveles de escolaridad y la percepcion de legalidad en Mexico? P1a: ??Hay variaciones en esta asociacion por sexo? – Debemos operacionalizar Voy a medir los niveles de escolaridad en grupos: 1: Hasta preparatoria 2: Licenciatura o posgrado Percepcion de legalidad sera medida usando la p1 “??Que tanto cree usted que se respetan o no se respetan las leyes en Mexico?”, Vamos a definir que una persona piensa positivamente (legalidad) si su respuesta fue “Mucho” o “Algo”
Entonces las variables de interes son: p1- Para legalidad s3- Para escolaridad s1- Sexo
Vamos a usar tambien la edad para tener cierto “control” s2- Edad
str(data$p1)
## Factor w/ 6 levels "Algo","Mucho",..: 1 5 1 1 2 6 1 1 1 5 ...
levels(data$p1)
## [1] "Algo" "Mucho" "NC" "NS" "Nada" "Poco"
table(data$p1)
##
## Algo Mucho NC NS Nada Poco
## 2588 492 19 95 3532 4274
str(data$s3)
## Factor w/ 10 levels "Carrera T\351cnica",..: 1 1 5 7 5 5 5 5 6 6 ...
levels(data$s3)
## [1] "Carrera T\351cnica" "NC"
## [3] "NS" "Ninguno"
## [5] "Preparatoria" "Primaria completa"
## [7] "Primaria incompleta" "Secundaria completa"
## [9] "Secundaria incompleta" "Universidad y m\341s"
table(data$s3)
##
## Carrera T\351cnica NC NS
## 713 21 4
## Ninguno Preparatoria Primaria completa
## 570 1805 1788
## Primaria incompleta Secundaria completa Secundaria incompleta
## 1363 2640 787
## Universidad y m\341s
## 1309
str(data$s1)
## Factor w/ 2 levels "Hombre","Mujer": 1 1 1 1 1 1 1 1 1 1 ...
levels(data$s1)
## [1] "Hombre" "Mujer"
table(data$s1)
##
## Hombre Mujer
## 4858 6142
str(data$s2)
## Factor w/ 81 levels "18","19","20",..: 4 7 6 4 7 7 10 1 11 10 ...
levels(data$s2)
## [1] "18" "19" "20" "21" "22" "23" "24" "25"
## [9] "26" "27" "28" "29" "30" "31" "32" "33"
## [17] "34" "35" "36" "37" "38" "39" "40" "41"
## [25] "42" "43" "44" "45" "46" "47" "48" "49"
## [33] "50" "51" "52" "53" "54" "55" "56" "57"
## [41] "58" "59" "60" "61" "62" "63" "64" "65"
## [49] "66" "67" "68" "69" "70" "71" "72" "73"
## [57] "74" "75" "76" "77" "78" "79" "80" "81"
## [65] "82" "83" "84" "85" "86" "87" "88" "89"
## [73] "90" "91" "92" "93" "94" "96" "97" "98"
## [81] "NS/NC"
table(data$s2)
##
## 18 19 20 21 22 23 24 25 26 27 28 29
## 277 255 253 226 241 240 252 226 185 213 241 237
## 30 31 32 33 34 35 36 37 38 39 40 41
## 285 154 228 212 191 204 244 216 248 218 335 135
## 42 43 44 45 46 47 48 49 50 51 52 53
## 299 207 178 280 165 185 274 200 274 147 228 192
## 54 55 56 57 58 59 60 61 62 63 64 65
## 184 151 200 167 177 139 174 95 126 119 85 135
## 66 67 68 69 70 71 72 73 74 75 76 77
## 85 87 110 53 117 45 69 53 61 56 64 36
## 78 79 80 81 82 83 84 85 86 87 88 89
## 39 36 47 15 14 27 23 15 9 12 5 7
## 90 91 92 93 94 96 97 98 NS/NC
## 5 3 2 1 2 2 1 1 1
Hagamos algunos cruces de variables
table(data$s3,data$p1)
##
## Algo Mucho NC NS Nada Poco
## Carrera T\351cnica 180 36 1 1 216 279
## NC 4 2 1 0 6 8
## NS 1 0 0 0 1 2
## Ninguno 104 42 5 15 216 188
## Preparatoria 454 89 2 9 547 704
## Primaria completa 368 77 2 13 647 681
## Primaria incompleta 283 81 1 31 461 506
## Secundaria completa 667 89 2 17 785 1080
## Secundaria incompleta 240 34 4 7 217 285
## Universidad y m\341s 287 42 1 2 436 541
Aqui creo mis variables
# Mi variable indicadora de legalidad:
data$v1<-NaN
table(data$v1, exclude=F)
##
## NaN
## 11000
data$v1<-ifelse(data$p1=="Mucho" | data$p1=="Algo",1,data$v1)
table(data$v1, exclude=F)
##
## 1 NaN
## 3080 7920
data$v1<-ifelse(data$p1=="Poco" | data$p1=="Nada",2,data$v1)
table(data$v1, exclude=F)
##
## 1 2 NaN
## 3080 7806 114
prop.table(table(data$v1))
##
## 1 2
## 0.2829322 0.7170678
# Aqui, mi variable de escolaridad
data$v2<-NaN
table(data$v2, exclude=F)
##
## NaN
## 11000
data$v2<-ifelse(data$s3=="Ninguno" | data$s3=="Primaria incompleta" |
data$s3=="Primaria completa" | data$s3=="Secundaria incompleta" |
data$s3=="Secundaria completa" | data$s3=="Carrera T\351cnica" |
data$s3=="Preparatoria",1,data$v1)
data$v2<-ifelse(data$s3=="Universidad y m\341s",2,data$v2)
prop.table(table(data$v2))
##
## 1 2
## 0.8794436 0.1205564
Hay que poner atencion a lo que pasa con los acentos, esto varia entre sistemas operativos Hay que revisar como sale para el caso de “Tecnica” y “Universidad y mas”
Podemos hacer algunos analisis
prop.table(table(data$v1,data$v2),2)
##
## 1 2
## 1 0.2876712 0.2486772
## 2 0.7123288 0.7513228
prop.table(table(data$v1,data$v2),2)
##
## 1 2
## 1 0.2876712 0.2486772
## 2 0.7123288 0.7513228
table1<-prop.table(table(data$v1,data$v2),2)
barplot(table1, main="Percepcion de legalidad \nsegun escolaridad",cex.main=1.5,
xlab="Escolaridad",cex.lab=1, names.arg=c("Hasta preparatoria","Superior y mas"),
cex.names=0.8,beside=T,col=c("darkblue","red"))
Y si lo queremos por sexo? Por el momento lo mas facil es hacerlo por partes…
table(data$s1)
##
## Hombre Mujer
## 4858 6142
# Para crear "bases con casos especificos"
dataH<-data[which(data$s1=="Hombre"),]
dataM<-data[which(data$s1=="Mujer"),]
prop.table(table(dataH$v1,dataH$v2),2)
##
## 1 2
## 1 0.2964763 0.2468085
## 2 0.7035237 0.7531915
table1<-prop.table(table(dataH$v1,dataH$v2),2)
barplot(table1, main="Percepcion de legalidad \nsegun escolaridad \nHombres",cex.main=1.5,
xlab="Escolaridad",cex.lab=1, names.arg=c("Hasta preparatoria","Superior y mas"),
cex.names=0.8,beside=T,col=c("darkblue","red"))
prop.table(table(dataM$v1,dataM$v2),2)
##
## 1 2
## 1 0.2810206 0.2508091
## 2 0.7189794 0.7491909
table1<-prop.table(table(dataM$v1,dataM$v2),2)
barplot(table1, main="Percepcion de legalidad \nsegun escolaridad \nMujeres",cex.main=1.5,
xlab="Escolaridad",cex.lab=1, names.arg=c("Hasta preparatoria","Superior y mas"),
cex.names=0.8,beside=T,col=c("darkblue","red"))