Skip to content

Commit

Permalink
c
Browse files Browse the repository at this point in the history
  • Loading branch information
jinseob2kim committed Apr 21, 2022
1 parent 079c74f commit 6681236
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ R 은 단순한 통계프로그램이 아닌 데이터분석 전 과정을 포
|7| 4월 4일| R 통계(1): [의학연구에서의 기술통계](https://blog.zarathu.com/posts/2020-07-08-table1inmed/), [code](code/descriptive.R), [tableone 소개](https://blog.zarathu.com/posts/2022-02-07-tableone/), [gtsummary 소개](https://blog.zarathu.com/posts/2022-02-07-gtsummary/)|
|8| 4월 11일| 중간고사 |
|9| 4월 18일| R 통계(2): [회귀분석, 생존분석](https://blog.zarathu.com/posts/2020-07-22-regressionbasic/), [생존분석 추가자료](https://blog.zarathu.com/posts/2020-10-29-survivalpractice/), [리뷰어 위한 의학통계](https://jinseob2kim.github.io/lecture-general/statreview/#59)
|10| 4월 25일| R 통계(3): [성향점수 매칭, 가중치](code/table1_ps.R), 국건영데이터 소개 |
|10| 4월 25일| R 통계(3): [성향점수 매칭, 가중치](code/table1_ps.R), [국건영데이터 소개](https://knhanes.kdca.go.kr/knhanes/sub03/sub03_01.do), [code](code/KNHANES.R) |
|11| 5월 2일| R로 보고서쓰기: [Rmarkdown](https://blog.zarathu.com/posts/2019-01-03-rmarkdown/), [엑셀, ppt로 저장하기](lecture/dataexport.pptx) |
|12| 5월 9일| R로 홈페이지/블로그 만들기: [blogdown](https://pkgs.rstudio.com/blogdown/), [distill](https://rstudio.github.io/distill/) |
|13| 5월 16일| R로 웹앱 만들기: [Shiny 소개](https://github.com/jinseob2kim/shiny-workshop-odsc2019) |
Expand Down
44 changes: 44 additions & 0 deletions code/KNHANES.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
library(survey)
a <- haven::read_sav("data/hn20_all.sav")

## See variable label
names(a)
sapply(a, function(x){attr(x, "label")})

## For practice: some variable
a <- haven::read_sav("data/hn20_all.sav", col_select = c("year", "sex", "age", "allownc", "ainc", "marri_1", "edu", "EC1_1",
"HE_sbp", "HE_dbp", "HE_glu", "HE_HCHOL", "HE_BMI", "psu", "kstrata", "wt_itvex", "wt_tot"))

sapply(a, function(x){attr(x, "label")})


var.factor <- c("sex", "edu", "allownc", "marri_1", "EC1_1", "HE_HCHOL")
for (v in var.factor){
a[[v]] <- factor(a[[v]])
}

## survey object
data.design <- svydesign(ids = ~psu, strata = ~kstrata, weights = ~wt_itvex, data = a) ## Error: missing in weights
data.design <- svydesign(ids = ~psu, strata = ~kstrata, weights = ~wt_itvex, data = filter(a, !is.na(wt_itvex)))

## Table 1
library(jstable)
CreateTableOneJS(vars = setdiff(names(a), c("year", "kstrata", "psu", "wt_itvex", "wt_tot")), strata = "sex", data = a)
svyCreateTableOneJS(vars = setdiff(names(a), c("year", "kstrata", "psu", "wt_itvex", "wt_tot")), strata = "sex", data = data.design)



## Regression
reg1 <- glm(HE_glu ~ age + sex + HE_BMI + edu + marri_1, data = a)
glmshow.display(reg1)
reg2 <- svyglm(HE_glu ~ age + sex + HE_BMI + edu + marri_1, design = data.design)
svyregress.display(reg2)

## Logistic
reg1 <- glm(HE_HCHOL ~ age + sex + HE_BMI + edu + marri_1, data = a, family = binomial)
glmshow.display(reg1)
reg2 <- svyglm(HE_HCHOL ~ age + sex + HE_BMI + edu + marri_1, design = data.design, family = quasibinomial())
svyregress.display(reg2)



22 changes: 21 additions & 1 deletion exam/mid.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,29 @@

## 데이터를 읽은 후
## Problem 1: "Q_" 로 시작하는 변수는 범주형(factor)으로, 나머지 변수는 숫자로 만들어라.
library(data.table);library(magrittr);library(ggpubr)

a <- fread("https://raw.githubusercontent.com/jinseob2kim/R-skku-biohrs/main/data/example_g1e.csv")
var.factor <- grep("Q_", names(a), value = T)

for (v in var.factor){
a[[v]] <- factor(a[[v]])
}

a[, (var.factor) := lapply(.SD, factor), .SD = var.factor]

sapply(a, class)


## Problem 2: RN_INDI, HME_YYYYMM 제외한 모든 연속변수의 연도별 평균과 표준편차를 구하라.
a[, .SD, .SDcols = -c("RN_INDI", "HME_YYYYMM", var.factor)][, lapply(.SD, sd, na.rm = T), keyby = "EXMD_BZ_YYYY"]



## Problem 3: 연도별 FBS의 Boxplot을 그리고 pptx로 저장하라.

dd <- ggboxplot(a, "EXMD_BZ_YYYY", "FBS")

## Problem 3: 연도별 FBS의 Boxplot을 그리고 pptx로 저장하라.
plot_file <- read_pptx() %>%
add_slide() %>% ph_with(dml(ggobj = dd), location=ph_location_type(type="body"))
print(plot_file, target = "plot_file.pptx")

0 comments on commit 6681236

Please sign in to comment.