-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.Rmd
209 lines (166 loc) · 6.63 KB
/
test.Rmd
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
---
title: '11'
output: html_document
date: "2022-10-05"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
library(tidyverse)
library(gapminder) # dataset
library(finalfit)
library(broom)
library(gtsummary)#install.packages("gtsummary")
#library(sjmic)#install.packages("sjmic")
library(sjlabelled)#install.packages("sjlabelled")
library(lubridate)#install.packages("lubridate")
library(sjlabelled)
library(psych)
library(Hmisc)
library(expss)#install.packages("expss")
```
## 1.2 data set ready
```{r}
# Import the data I worked on for weeks
USHSv2 <- read_csv("USHSv2.csv")
#Some of the variables I plan to use was not selected into my data set.
#I'll get the raw data and merge them.
USHS <- read.csv("daF3224e.csv", sep = ";")
USHSv2 <- left_join(USHS, USHSv2) # USHSv2 is still the name.
#select the variables I'm going to use.
```
## 1.3 Generating new variables for modelling
### 1.3.1 Generating variable about 1-year medical help seeking practice
```{r, eval = FALSE}
# questions under k37 collected information about whether and how much medical
#help seeking behaviors (MHSB) the respondents had for the past one year for
#various type of medical providers. Here I simplified them into whether they
#have MHSB for any type of service.
#select columns k37s
col_k37s <- USHSv2 %>% select (starts_with("k37"))
#How many types of medical provider he/she saw 1~5 times last year.
col_k37s <- col_k37s %>%
rowwise() %>%
mutate(med_seek_face_few =sum(
across (starts_with("k37")&contains("_2_")) == 1, na.rm = T))%>%
ungroup() %>%
apply_labels(., med_seek_face_few = "Medical visits a few times")
#How many types of medical provider he/she saw >5 times last year.
#a new variable
col_k37s <- col_k37s %>%
rowwise() %>%
mutate(med_seek_face_many =sum(
across (starts_with("k37")&contains("_2_")) == 2, na.rm = T))%>%
ungroup() %>%
apply_labels(., med_seek_face_many = "Medical vistis many times")
#How many types of medical service he/she saw via phone last year.
#a new variable
col_k37s <- col_k37s %>%
rowwise() %>%
mutate(med_seek_phone =sum(
across (starts_with("k37")&contains("_3_")) == 3, na.rm = T))%>%
ungroup() %>%
apply_labels(., med_seek_phone = "Medical phone call")
#How many types of medical service he/she saw via internet last year.
#a new variable
col_k37s <- col_k37s %>%
rowwise() %>%
mutate(med_seek_online =sum(
across (starts_with("k37")&contains("_4_")) == 4, na.rm = T))%>%
ungroup() %>%
apply_labels(., med_seek_online = "Medical help seeking on-line")
#If he/she sought any medical help during last year (yes/no)
#This is a combination of the four new variables above, if any of them has
#a value of 1, this new variable also gets a value of 1, or else it gets 0.
col_k37s <- col_k37s %>%
rowwise() %>%
mutate(med_seek_any =sum(across (starts_with("med_seek")), na.rm = T)) %>%
mutate(med_seek_any = if_else(med_seek_any == 0, 0, 1)) %>%
ungroup() %>%
apply_labels(., med_seek_any = "Any medical help seeking behavior")
col_k37s
USHSv2_col_k37s <- full_join(USHSv2, col_k37s)
USHSv2_col_k37s %>% select(starts_with("med_seek"))
write_csv(USHSv2_col_k37s, "USHSv2_col_k37s")
```
Temporary chunk
```{r}
USHSv2 <- read_csv("USHSv2_col_k37s")
USHSv2
```
### 1.3.2 Generating variable about length of sedentary hours
```{r}
#items k42s asked how long do you sit for various purposes in a typical day
#answers blended hours and minutes. Here I unified their unit into hours.
#pass variable names of items under k42 into a vector
columns <- USHSv2 %>% select(starts_with("k42")) %>% names()
#check their variable type, finding they are characters.
sapply(USHSv2[,columns], class)
#transform into time(period) variable and check if done
USHSv2[,columns] <- lapply(USHSv2[,columns], function(x)hms(x, quiet = T))
sapply(USHSv2[,columns], class)
USHSv2[,columns]
#transform minutes into hours and added up hour with transformed hours
#hereby 6 variables were generated, each being the length of sitting for
#a type of behavior, see variable labels
USHSv2 <- USHSv2 %>%
mutate(sit_study = hour(k42_1)+ minute(k42_1)/60,
sit_work = hour(k42_2)+ minute(k42_2)/60,
sit_monitor = hour(k42_3)+ minute(k42_3)/60,
sit_read = hour(k42_4)+ minute(k42_4)/60,
sit_transport = hour(k42_5)+ minute(k42_5)/60,
sit_else = hour(k42_6)+ minute(k42_6)/60) %>%
apply_labels(., sit_work = "sitting hours for work",
sit_monitor = "sitting hours before a monitor",
sit_read = "sitting hours for reading",
sit_transport = "sitting hours during transportation",
sit_else = "sitting hours for reasons other than the above")
USHSv2 %>% select(starts_with("sit"))
#Generate a variable about the overall length of sitting for a week of work days,
#irrespective of purposes.
USHSv2 <- USHSv2 %>%
mutate(sedentary_length = rowSums(select(., starts_with("sit_")), na.rm = T)) %>%
apply_labels(., sedentary_length = "overall sitting hours for working days of a week")
#aother way to add variable. Keep record here.
#var_lab(USHSv2$k42_1_hm) = "study sitting time"
#var_lab(USHSv2$k42_2_hm) = "work sitting time"
#var_lab(USHSv2$k42_3_hm) = "TV&computer sitting time"
#var_lab(USHSv2$k42_4_hm) = "reading sitting time"
#var_lab(USHSv2$k42_5_hm) = "transportation sitting time"
#var_lab(USHSv2$k42_6_hm) = "other sitting time"
#check if their variable are there
k42_cols <- USHSv2 %>% select(starts_with("sit")) %>% names()
k42_cols
lapply(USHSv2[k42_cols], function(x)var_lab(x))
#check the value
USHSv2 %>% select(contains("sit"))
USHSv2 %>% select(sedentary_length)
ss <- c("a", "b", "c", "d")
df <-data.frame(a = c(1,2,3,4), b =c(5,6,7,8), c=c(9,10,11,13))
da <- df %>% rowwise() %>% mutate(d = sd(across(everything())))
da <- da %>% mutate(e=sum(across(c(a, b, c, d))))
da
row
vec <- c(2,6,10)
vec
sd(vec)
df1 <- data.frame(no = c(1:5), b = c("a","b","c","d","e"), c = 36:40)
df2 <- data.frame(no = c(1:5), b = c("a","b","c","d","e"), d = 100:104)
full_join(df1, df2)
df1 <- data.frame(no = c(1:5), b = c("a","b","c","d","e"), c = 36:40)
df1
df1 <- df1 %>%
rename(bb =b, cc = c)
USHSv2 <- read_csv("USHSv2.csv")
USHSv2
USHSv2 <- USHSv2 %>%
rename(bv3b = bv3,
bv6v = bv6)
```
## Including Plots
```{r}
```