0. 必要パッケージの導入とデータの取りこみ

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.1
## ✔ readr   2.1.2     ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(ggplot2)
library(rmarkdown)
library(readxl)
library(coin)
##  要求されたパッケージ survival をロード中です
library(exactRankTests)
##  Package 'exactRankTests' is no longer under development.
##  Please consider using package 'coin' instead.
## 
## 
##  次のパッケージを付け加えます: 'exactRankTests' 
## 
##  以下のオブジェクトは 'package:coin' からマスクされています:
## 
##     dperm, pperm, qperm, rperm
setwd("C:/Users/eishin/Desktop/biostat2")
ds <- read_excel("C:/Users/eishin/Desktop/biostat2/サンプルデータ.xls", na = '.', 1)

1. ① TCの比較に対する母平均の差の検定

1-1. データの分布の確認

hist(ds$TC)

boxplot(ds$TC)

正規分布であると判断する

1-2. 標準治療群と新治療群とで分類する

tmp1<- ds[c("TC", "治療法")] %>% filter(治療法==1)
tmp2<- ds[c("TC", "治療法")] %>% filter(治療法==2)

1-3.等分散性の確認

var.test(tmp1$TC, tmp2$TC)
## 
##  F test to compare two variances
## 
## data:  tmp1$TC and tmp2$TC
## F = 1.1416, num df = 125, denom df = 111, p-value = 0.4767
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.7923056 1.6383808
## sample estimates:
## ratio of variances 
##           1.141601

F検定の結果、「2群間の分散に差は無い」というH0を棄却しないため、

等分散性を仮定できる

1-4. Student’s t-testを行う

t.test(tmp1$TC, tmp2$TC, var.equal=TRUE)
## 
##  Two Sample t-test
## 
## data:  tmp1$TC and tmp2$TC
## t = 0.74948, df = 236, p-value = 0.4543
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -5.814518 12.955049
## sample estimates:
## mean of x mean of y 
##  193.7099  190.1397

標準治療群と新治療群間で、TCの母平均に差はなかった

2. ② TGの比較に対する母平均の差の検定

2-1. データの分布の確認

hist(ds$TG)

boxplot(ds$TG)

正規分布では無さそうだが、一応正規性の検定をして確認する

shapiro.test(ds$TG)
## 
##  Shapiro-Wilk normality test
## 
## data:  ds$TG
## W = 0.66141, p-value < 2.2e-16

shapiro-wilk検定ではH0: 正規分布 なので、帰無仮説が棄却→データは正規分布とは言えない

2-2. 母平均の差の検定

tmp3<- ds[c("TG", "治療法")] %>% filter(治療法==1)
tmp4<- ds[c("TG", "治療法")] %>% filter(治療法==2)
var.test(tmp3$TG, tmp4$TG)
## 
##  F test to compare two variances
## 
## data:  tmp3$TG and tmp4$TG
## F = 0.82935, num df = 125, denom df = 111, p-value = 0.3091
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.5755962 1.1902551
## sample estimates:
## ratio of variances 
##          0.8293529
wilcox.test(tmp3$TG, tmp4$TG, paired=FALSE)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  tmp3$TG and tmp4$TG
## W = 7135, p-value = 0.8823
## alternative hypothesis: true location shift is not equal to 0

標準治療群と新治療群間で、TGの母平均に差はなかった

3.③ TC5年後変化量の正負の比較に対する独立性の検定

3-1. 変化量正負の行をつくる

ds1<-mutate(ds, 変化量=(5年後のTC-TC))
ds2<-mutate(ds1, 変化量正負 = (変化量>=0)) %>% 
  mutate(正負= if_else(変化量正負, "1", "0"))

3-2. クロス集計表を作る

table<-table(ds2$変化量正負, ds2$治療法)
table
##        
##           1   2
##   FALSE 106  89
##   TRUE   20  23

総症例数や各セルの頻度は大きく、カイ2乗検定を行う

3-3. 独立性の検定

chisq.test(table)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  table
## X-squared = 0.58434, df = 1, p-value = 0.4446
chisq.test(table,correct=F)
## 
##  Pearson's Chi-squared test
## 
## data:  table
## X-squared = 0.87084, df = 1, p-value = 0.3507

連続修正の有無に関わらず、独立性の検定は棄却されない

「TC変化量の正負」と「治療法」は独立である

#4. ④ 共変量を治療法とした有効性(1:有効,0:無効)に関するロジスティック回帰

ds3<-ds[c("治療法","有効性")]
fit1<-glm(ds3$有効性~.,data=ds3,family=binomial(link="logit"))
summary(fit1)
## 
## Call:
## glm(formula = ds3$有効性 ~ ., family = binomial(link = "logit"), 
##     data = ds3)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.2858  -0.9925  -0.9925   1.0727   1.3744  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)   
## (Intercept)  -1.1553     0.4121  -2.803  0.00506 **
## 治療法        0.7033     0.2640   2.664  0.00771 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 329.11  on 237  degrees of freedom
## Residual deviance: 321.91  on 236  degrees of freedom
## AIC: 325.91
## 
## Number of Fisher Scoring iterations: 4

切片-1.1553, 治療法の回帰係数0.7033となった。

切片や傾きの回帰係数の検定は、どちらも棄却された