-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prepara os dados.R
168 lines (117 loc) · 3.67 KB
/
Prepara os dados.R
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
#Instala e carrega pacotes necessários
pacotes <- c("survey", "srvyr", "tidyverse", "devtools")
lapply(pacotes, install.packages)
lapply(pacotes, library, character.only = TRUE)
install_github("ajdamico/lodown", dependencies = TRUE)
library(lodown)
#Realiza download do catalogo da PNS 2013 e desta
pns_cat <-
get_catalog("pns" ,
output_dir = file.path(path.expand("~") , "PNS"))
pns_cat <- lodown("pns" , pns_cat)
#Carrega a PNS 2013
options(survey.lonely.psu = "adjust")
pns_design1 <-
readRDS(file.path(
path.expand("~") ,
"PNS" ,
"2013 long questionnaire survey design.rds"
))
pns_design <- as_survey_design(pns_design1)
#Adequação de variáveis
pns_design$variables$q002 <- as.factor(pns_design$variables$q002)
pns_design$variables$q030 <- as.factor(pns_design$variables$q030)
pns_design$variables$q074 <- as.factor(pns_design$variables$q074)
pns_design$variables$q063 <- as.factor(pns_design$variables$q079)
pns_design$variables$q068 <- as.factor(pns_design$variables$q063)
attach(pns_design$variables)
comorb_var <- data.frame(q002, q030, q074, q079, q063)
summary(comorb_var)
detach(pns_design$variables)
pns_design$variables$q002 <- na_if(pns_design$variables$q002, "")
pns_design$variables$q030 <- na_if(pns_design$variables$q030, "")
pns_design$variables$q002 <-
droplevels.factor(pns_design$variables$q002)
pns_design$variables$q030 <-
droplevels.factor(pns_design$variables$q030)
#Cria variável derivada: Comorbidade
pns_design <- mutate(
pns_design,
q002 = recode_factor(
q002,
'1' = "1",
'2' = "2",
'3' = "2"
),
q030 = recode_factor(
q030,
'1' = "1",
'2' = "2",
'3' = "2"
)
)
pns_design <-
update(pns_design ,
one = 1 ,
V1 = as.factor(
case_when(
q002 == "2" &
q030 == "2" &
q074 == "2" &
q079 == "2" &
q063 == "2" ~ "D0",
q002 == "1" &
q030 == "2" &
q074 == "2" &
q079 == "2" &
q063 == "2" ~ "D1",
q002 == "2" &
q030 == "1" &
q074 == "2" &
q079 == "2" &
q063 == "2" ~ "D1",
q002 == "2" &
q030 == "2" &
q074 == "1" &
q079 == "2" &
q063 == "2" ~ "D1",
q002 == "2" &
q030 == "2" &
q074 == "2" &
q079 == "1" &
q063 == "2" ~ "D1",
q002 == "2" &
q030 == "2" &
q074 == "2" &
q079 == "2" &
q063 == "1" ~ "D1",
TRUE ~ "D2"
)
))
pns_design$variables$V1
summary(pns_design$variables$V1)
#Cria e modifica variáveis
pns_design <- mutate(pns_design,
V2 = recode_factor(
p050,
'1' = "T1",
'2' = "T1",
"3" = "T2"
))
pns_design$variables$c009 <-
droplevels.factor(pns_design$variables$c009, exclude = 9)
pns_design <-
update(pns_design ,
one = 1 ,
V3 = as.factor(case_when(p035 < 3 ~ "S1",
p035 >= 3 ~ "S2")))
#Prepara dataframe para exportação
attach(pns_design$variables)
DADOS <-
data.frame(V1,
V2,
V3)
DADOS <- na.omit(DADOS)
detach(pns_design$variables)
#Exporta dados
save(DADOS, file = "DADOS.Rda")