-
Notifications
You must be signed in to change notification settings - Fork 4
/
.Rhistory
512 lines (512 loc) · 13.5 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n()) %.%
mutate(Prob= Mta/sum(Mta[ZONA=="Urbana"]))
arrange(ZONA)
head(UPMs)
UPMs
View(UPMs)
sum(Mta[ZONA=="Urbana"])
sum(UPMs$Mta[ZONA=="Urbana"])
sum(UPMs$Mta[UPMs$ZONA=="Urbana"])
UPMs<- df %.%
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n()) %.%
mutate(Prob= Mta/sum(Mta[ZONA=="Urbana"]))
arrange(ZONA)
head(UPMs)
#Generar el dataframe de UPMS y su respectivo tamaño
UPMs<- df %.%
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n()) %.%
mutate(Prob= Mta/sum(Mta[ZONA=="Urbana"])) %.%
arrange(ZONA)
head(UPMs)
by_species <- iris %>% group_by(Species)
by_species %>% summarise_each(funs(length))
by_species <- iris %>% group_by(Species)
by_species
by_species %>% summarise_each(funs(length))
by_species %>% mutate_each(funs(half = . / 2))
by_species %>% mutate_each(funs(half = . / 2))
by_species %>% mutate_each(funs(min_rank))
UPMs<- df %.%
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n())
UPMs
UPMs<- df %.%
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n()) %.%
mutate(Prob= Mta/sum(Mta[ZONA=="Urbana"]))
UPMs
UPMs<- df %.%
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n()) %.%
mutate(Prob= ifelse(ZONA=="Urbana",Mta/sum(Mta),0)) %.%
arrange(ZONA)
head(UPMs)
colsum(df$TamHog)
colsums(df$TamHog)
SumCol(df$TamHog)
colSums(df$TamHog)
df$TamHog
colSums(df$TamHog)
rowSums(df$TamHog)
UPMs<- df %.%
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n()) %.%
mutate(Prob= Mta/colsum(Mta[ZONA=="Urbana"])) %.%
arrange(ZONA)
head(UPMs)
UPMs<- df %.%
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n()) %.%
mutate(Prob= Mta/sum(Mta[ZONA=="Urbana"])) %.%
arrange(ZONA)
head(UPMs)
a<- UPMs$Mta/sum(UPMs$Mta[UPMs$ZONA=="Urbana"])
a
UPMs<- df %.%
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n()) %.%
mutate(Prob=ifelse(ZONA=="Urbana",Mta/sum($Mta),0)) %.%
arrange(ZONA)
head(UPMs)
UPMs<- df %.%
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n()) %.%
mutate(Prob=ifelse(ZONA=="Urbana",Mta/sum($Mta),0)) %.%
arrange(ZONA)
head(UPMs)
UPMs<- df %.%
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n()) %.%
mutate(Prob=ifelse(ZONA=="Urbana",Mta/sum(Mta),0)) %.%
arrange(ZONA)
head(UPMs)
UPMs
UPMs<- df %.%
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n()) %.%
arrange(ZONA)
head(UPMs)
UPMU<- UPMs %.%
filter(ZONA=="URBANA")
UPMU<- UPMs %.%
filter(ZONA=="URBANA")%.%
mutate(fa=Mta/sum(Mta))
UPMU<- UPMs %.%
filter(ZONA=="URBANA") %.%
mutate(fa=Mta/sum(Mta))
UPMU
UPMU<- UPMs %.%
filter(ZONA=="URBANA")
UPMU
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
mutate(fa=Mta/sum(Mta))
UPMU
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
mutate(fa=Mta/sum(UPMs$Mta))
UPMU
sum(UPMs$Mta)
sum(UPMU$Mta)
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
mutate(fa=Mta/sum(UPMU$Mta))
UPMU
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
mutate(fa=Mta/sum(UPMU$Mta))
UPMU
Estratos<- df %.%
select(ZONA,TamHog) %.%
group_by(ZONA) %.%
summarise(n=n(),
s=sd(TamHog)) %.%
mutate(p=n/sum(n))
Estratos
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
mutate(fa=Mta/sum(Mta))
UPMU
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
mutate(fa=Mta/mean(Mta))
UPMU
design1<-svydesign(data=muestrappt,id=~SEGMENTO+id,strata=~ZONA,pps="overton")
design1<-svydesign(data=muestrappt,id=~SEGMENTO+id,strata=~ZONA,pps="brewer")
UPMU
Desc(UPMU)
UPMU<- UPMs %.%
filter(ZONA=="Urbana")
UPMU
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
mutate(fa=Mta*3)
UPMU
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
mutate(fa=Mta/sum(Mta))
UPMU
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
mutate(fa=sum(Mta))
UPMU
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
group-by(ZONA) %.%
mutate(fa=Mta/sum(Mta))
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
group_by(ZONA) %.%
mutate(fa=Mta/sum(Mta))
UPMU
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
group_by(ZONA) %.%
mutate(fa=Mta/sum(Mta*aurbano))
UPMU
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
group_by(ZONA) %.%
mutate(fa=Mta/(sum(Mta)/aurbano))
UPMU
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
group_by(ZONA) %.%
mutate(fa=Mta/(sum(Mta)/aurbano),
fb=b/Mta)
UPMU
UPMR<- UPMs %.%
filter(ZONA=="Rural") %.%
group_by(ZONA) %.%
mutate(fa=Mta/(sum(Mta)/arural),
fb=b/Mta)
UPMR
UPMU
UPMs <- rbind(UPMU,UPMR)
UPMs
muestrappt<- left_join(muestrappt,UPMs,by="SEGMENTO")
View(muestrappt)
design1<-svydesign(data=muestrappt,id=~SEGMENTO+id,
strata=~ZONA,pps="brewer",prob=~fa+fb)
View(muestrappt)
UPMs
crime<-data.frame(crimtab)
dim(crime)
#genera semilla aleatoria
set.seed(1)
muestramia<- sample(1:nrow(crime),30,replace=FALSE)
muestramia
#Asignar los elementos de la muestra al data frame de datos
crimemuestramia<- crime[muestramia, ]
head(crimemuestramia)
library(SamplingUtil)
muestrasis<- sys.sample(nrow(crime),30)
muestrasis
#Asignar los elementos de la muestra al data frame de datos
crimemuestrasis<- crime[muestrasis, ]
head(crimemuestrasis)
#Cálculo del tamaño de muestra con error relativo
#Error relativo
r<-0.05
#Generando df de ENAHO2013 a nivel de hogar
library(dplyr)
df<- ENAHO2013 %.%
mutate(TamHog=as.numeric(TamHog),
phu=ifelse(TamHog>1,0,1)) %.%
select(FACTOR:ZONA,TamHog,phu,-REGION) %.%
group_by(SEGMENTO,CUESTIONARIO,HOGAR,ZONA) %.%
summarise(TamHog=mean(TamHog),
phu=mean(phu)) %.%
mutate(id=paste0(SEGMENTO,CUESTIONARIO,HOGAR,ZONA))
nsizeR<- nsize(x=df$TamHog,r=r,alpha=0.05)
nsizeR
#Cálculo del tamaño de muestra con error absoluto
#Igualar el error absoluto con el error relativo
abs<-mean(df$TamHog)*r
nsizeA<- nsize(x=df$TamHog,abs=abs,alpha=0.05)
nsizeA
#Cálculo de tamaño de muestra para proporción
nsizeP<- nsize(x=df$phu,abs=0.02,alpha=0.05)
nsizeP
crime<-data.frame(crimtab)
dim(crime)
#Selección de la muestra
#genera semilla aleatoria
set.seed(1)
muestramia<- sample(1:nrow(crime),30,replace=FALSE)
muestramia
#Asignar los elementos de la muestra al data frame de datos
crimemuestramia<- crime[muestramia, ]
head(crimemuestramia)
#Cargar libreria:
library(SamplingUtil)
muestrasis<- sys.sample(nrow(crime),30)
muestrasis
#Asignar los elementos de la muestra al data frame de datos
crimemuestrasis<- crime[muestrasis, ]
head(crimemuestrasis)
#Cálculo del tamaño de muestra con error relativo
#Error relativo
r<-0.05
#Generando df de ENAHO2013 a nivel de hogar
library(dplyr)
df<- ENAHO2013 %.%
mutate(TamHog=as.numeric(TamHog),
phu=ifelse(TamHog>1,0,1)) %.%
select(FACTOR:ZONA,TamHog,phu,-REGION) %.%
group_by(SEGMENTO,CUESTIONARIO,HOGAR,ZONA) %.%
summarise(TamHog=mean(TamHog),
phu=mean(phu)) %.%
mutate(id=paste0(SEGMENTO,CUESTIONARIO,HOGAR,ZONA))
nsizeR<- nsize(x=df$TamHog,r=r,alpha=0.05)
nsizeR
#Cálculo del tamaño de muestra con error absoluto
#Igualar el error absoluto con el error relativo
abs<-mean(df$TamHog)*r
nsizeA<- nsize(x=df$TamHog,abs=abs,alpha=0.05)
nsizeA
#Cálculo de tamaño de muestra para proporción
nsizeP<- nsize(x=df$phu,abs=0.02,alpha=0.05)
nsizeP
#Cálculo de las proporciones por estrato
Estratos<- df %.%
select(ZONA,TamHog) %.%
group_by(ZONA) %.%
summarise(n=n(),
s=sd(TamHog)) %.%
mutate(p=n/sum(n))
Estratos
#Asignación de la muestra proporcional a los estratos
nsizeProp<-nstrata(nsizeR[[2]],Estratos[,4],method="proportional")
nsizeProp
#Asignación de la muestra óptima a los estratos
#Costo de entrevista por estrato
ch<-c(5,10)
nsizeOpt<-nstrata(nsizeR[[2]],Estratos[,4],Estratos[,3],ch,method="optimal")
nsizeOpt
#Asignación de la muestra óptima a los estratos(asume costos iguales)
nsizeNeyman<-nstrata(nsizeR[[2]],Estratos[,4],Estratos[,3],method="neyman")
nsizeNeyman
#genera semilla aleatoria
set.seed(1)
muestra<- sample(1:nrow(df),nsizeR[[2]],replace=FALSE)
#Asignar los elementos de la muestra al data frame de datos
muestra<- df[muestra,]
#Carga de paquete DescTools
library(DescTools)
#Intervalos de confianza 95% clásicos
ICTamHog<- MeanCI(muestra$TamHog, trim = 0,conf.level = 0.95, na.rm = FALSE)
ICTamHog
#Media del total de la encuesta
mean(df$TamHog)
#Diferencia relativa
difR<-paste0(abs(round((ICTamHog[1]-mean(df$TamHog))/mean(df$TamHog),3))*100,"%")
#Generar el dataframe de UPMS y su respectivo tamaño
UPMs<- df %.%
mutate(TamHog=as.numeric(TamHog)) %.%
select(SEGMENTO,ZONA,TamHog) %.%
group_by(SEGMENTO,ZONA) %.%
summarise(Mta=n()) %.%
arrange(ZONA)
head(UPMs)
#Se asigna proporcionalmente la muestra a cada estrato
#Recordemos la distribución proporcional de los estratos:
Estratos
nurbano<- ceiling(nsizeR[[2]]*Estratos[1,4])
nrural<- ceiling(nsizeR[[2]]*Estratos[2,4])
nurbano;nrural
#Cálculo del número de UPMs por estrato
b<-5
aurbano<- ceiling(nurbano/b)
arural<- ceiling(nrural/b)
#Unir en un solo objeto para facilitar input de función
asizeppt<-rbind(aurbano,arural)
asizeppt
#Selección de la muestra con PPT(sistemática)
library(pps)
muestrappt<-ppssstrat(UPMs$Mta,UPMs$ZONA,asizeppt)
muestraUPMs<-UPMs[muestrappt,]
#Muestra de la selección
head(muestraUPMs)
tail(muestraUPMs)
#Verificar selección por estrato
Freq(muestraUPMs$ZONA)
#Procedimiento para seleccionar submuestras en cada cluster de tamaño fijo
#Listado de segmentos seleccionados
segs<- unique(muestraUPMs$SEGMENTO)
sample<- list()
#Identifica el número de columna del id(se requiere para el ciclo)
idcolnum<-which( colnames(df)=="id" )
#Ciclo para seleccionar la muestra en cada segmento
for(i in 1:length(segs)){
subsample<-df[which(df$SEGMENTO==segs[i]),]
sample[[i]]<-sys.sample(nrow(subsample),b)
#Previene de los números que se pasan del total del segmento
#if(sample[[i]][b]>nrow(subsample)){
# sample[[i]][b]<-1
# }
#Asigna en cada elemento de las submuestras los elem. seleccionados
sample[[i]]<-subsample[unlist(sample[[i]]),idcolnum]
}
#Genera el data frame de ids seleccionados(ya que estaban en una lista)
sampledf<-data.frame(id=unlist(sample))
#Uniendo el data frame de datos con la muestra seleccionada mendiante la llave creada
muestrappt<-inner_join(unique(sampledf),unique(df),by="id")
Freq(muestrappt$ZONA)
UPMU<- UPMs %.%
filter(ZONA=="Urbana") %.%
group_by(ZONA) %.%
mutate(fa=Mta/(sum(Mta)/aurbano),
fb=b/Mta)
#Estrato Rural
UPMR<- UPMs %.%
filter(ZONA=="Rural") %.%
group_by(ZONA) %.%
mutate(fa=Mta/(sum(Mta)/arural),
fb=b/Mta)
#Unir ambos estratos
UPMs <- rbind(UPMU,UPMR)
#Incluir probabilidades en data frame de muestra
muestrappt2<- left_join(muestrappt,UPMs,by=c("SEGMENTO","ZONA"))
View(muestrappt2)
library(survey)
design1<-svydesign(data=muestrappt,id=~SEGMENTO+id,
strata=~ZONA,pps="brewer",prob=~fa+fb)
summary(design1)
design1<-svydesign(data=muestrappt2,id=~SEGMENTO+id,
strata=~ZONA,pps="brewer",prob=~fa+fb)
summary(design1)
design1
muestrappt2
TamHogmc<- svymean(~TamHog,design=design1,deff=TRUE)
TamHogmc
TamHogmcdeff<-deff(TamHogmc)
TamHogmcdeff
confint(TamHogmc)
cv(TamHogmc)
TamHogmc
difR<-paste0(abs(round((TamHogmc[1]-mean(df$TamHog))/mean(df$TamHog),3))*100,"%")
difR
View(muestrappt2)
View(df)
ENAHO2013
str(ENAHO2013)
str(ENAHO2013)
design2<-svydesign(data=muestrappt2,id=~SEGMENTO+id,
strata=~ZONA,pps="brewer",weigth=~Mta)
design2<-svydesign(data=muestrappt2,id=~SEGMENTO+id,
strata=~ZONA,pps="brewer",weights=~Mta)
design2
summary(design2)
TamHogmc<- svymean(~TamHog,design=design2,deff=TRUE)
TamHogmc
TamHogmc<- svymean(~TamHog,design=design1,deff=TRUE)
TamHogmc
library(dplyr)
crimemuestramia2<- crime %.%
sample(n,replace=FALSE)
crime<-data.frame(crimtab)
dim(crime)
crimemuestramia2<- crime %.%
sample(n,replace=FALSE)
n<-30
crimemuestramia2<- crime %.%
sample(n,replace=FALSE)
dim(crime)
crime
crimemuestramia2<- crime %.%
sample(size=n,replace=FALSE)
crimemuestramia2<- crime %.%
sample_n(size=n,replace=FALSE)
crimemuestramia2
crimemuestramia3<- crime %.%
sample_n(size=n,weight=Freq)
crimemuestramia3
#Muestra con una proporción de casos
crimemuestramia3<- crime %.%
sample_frac(0,01)
crimemuestramia4
crimemuestramia4<- crime %.%
sample_frac(0,01)
crimemuestramia4
#Muestra con una proporción de casos
crimemuestramia4<- crime %.%
sample_frac(0.05)
crimemuestramia4
head(crimemuestramia4)
head(crimemuestramia2)
#Muestra sin reemplazo
crimemuestramia2<- crime %.%
sample_n(size=n,replace=FALSE)
head(crimemuestramia2)
crimemuestramia2<- crime %.%
sample_n(size=n,replace=FALSE)
library(dplyr)
#Muestra sin reemplazo
crimemuestramia2<- crime %.%
sample_n(size=n,replace=FALSE)
head(crimemuestramia2)
library(dplyr)
#Muestra sin reemplazo
crimemuestramia2<- crime %.%
sample_n(size=n,replace=FALSE)
crime<-data.frame(crimtab)
dim(crime)
#Muestra sin reemplazo
crimemuestramia2<- crime %.%
sample_n(size=n,replace=FALSE)
head(crimemuestramia2)
crimemuestramia2<- crime %.%
sample_n(size=n,replace=FALSE)
n<-30
#Muestra sin reemplazo
crimemuestramia2<- crime %.%
sample_n(size=n,replace=FALSE)
head(crimemuestramia2)
#Muestra con pesos
crimemuestramia3<- crime %.%
sample_n(size=n,weight=Freq)
head(crimemuestramia3)
#Muestra con una proporción de casos
crimemuestramia4<- crime %.%
sample_frac(0.05)
head(crimemuestramia4)
library(devtools)
install_github("knitcitations", "cboettig")
library(knitcitations)
r <- ref("110.1002/9780470580066.ch1")
print(r, "html")
library(knitcitations)
r <- ref("110.1002/9780470580066.ch1")
library("knitcitations", lib.loc="C:/Program Files/R/R-3.1.1/library")
r <- ref("110.1002/9780470580066.ch1")
citet("110.1002/9780470580066.ch1")
newbib()
library(knitcitations)