-
Notifications
You must be signed in to change notification settings - Fork 0
/
pharm_in_R.Rmd
156 lines (99 loc) · 3.51 KB
/
pharm_in_R.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
---
title: "pharm in R"
author: "liuc"
date: '2022-07-07'
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## pharm in R
> https://github.com/insightsengineering
R语言应用范围中,Pharma相关的应用是一个很大很重要的领域,考虑到`Rstudio`, `Shiny`在工作中的方便性,R语言和SAS在临床 药物研发等领域还是有的一比的,当然简单易用同时兼顾强大还需要很多优秀的开发人员。
在几家药企的努力下,基于R平台的分析在药企中使用会愈发频繁,此处几个不错的R包的使用,并和SAS进行一些对比。
```{r}
library(pharmverse)
library(mmrm)
```
## 下面以mmrm包为例
此包是用来做重复测量资料的混合效应模型的。在SAS和Stata的混合效应模型中,可以选择covariance matrix,KR(Kenward & Roger) style ajustment fit an unstructured covariance matrix MMRM model.
`lme4`似乎不是很方便,而此包较为方便得到和SAS一致的结果。
其有多种covariance可供选择,以及Satterthwaite adjusted degrees of freedom,Kenward-Roger adjusted degrees of freedom and coefficients covariance matrix 等。详细可参见其官方文档。
其文档所说的scope为:
1,response 残差为正态分布
2,Marginal linear models (no individual-level random effects). 这个就不是很懂了,不应该主要关注个体间的随机效应吗?
此包最为重要的部分私以为在于其control部分的应用:
```{r}
# ?mmrm_control
mmrm_control(
method = "Kenward-Roger",
optimizer = c("L-BFGS-B", "BFGS"),
n_cores = 2,
start = c(0, 1, 1, 0, 1, 0),
accept_singular = FALSE,
drop_visit_levels = FALSE
)
```
下面只考虑个体检的随机效应,报错。
```{r}
fit <- mmrm(formula = score ~ time + class + AGE + us(1|`PatientID`),
data = df2,
reml = TRUE,
method = "Kenward-Roger"
)
```
Note that if you would like to match SAS results for an unstructured covariance model, you can use the linear Kenward-Roger approximation:
```{r}
fit2 <- mmrm(formula = score ~ time * class + AGE + us(time|`PatientID`),
data = df2,
reml = TRUE,
method = "Kenward-Roger-Linear"
)
```
```{r}
summary(fit2)
```
*Hypothesis testing:*
一维contrast的两种算法在freedom上的结果是一致的,但标准误等不一致。此处所讨论到的contrast,在应用到`emmeans`包的时候需要考虑些啥么。
一维和多维linear 是啥意思。。
```{r}
contrast <- numeric(length(component(fit, "beta_est")))
contrast[3] <- 1
df_1d(fit, contrast)
```
Multi-dimensional contrasts:
```{r}
contrast <- matrix(data = 0, nrow = 2, ncol = length(component(fit, "beta_est")))
contrast[1, 2] <- contrast[2, 3] <- 1
df_md(fit, contrast)
```
```{r}
emmeans::emmeans(fit, ~ ARMCD | AVISIT)
```
```{r}
emm_res <- emmeans::emmeans(fit, ~ ARMCD | AVISIT)
bb <- contrast(emm_res, method = 'trt.vs.ctrl',
type = 'response', adjust = 'fdr',
by = 'ARMCD',
infer = c(TRUE, TRUE))
plot(bb) +
theme_bw() +
geom_vline(xintercept = 0, linetype = 2, color = 'red4')
```
## R for clinical study reports
```{r}
# 写出RTF格式的表格
library(r2rtf)
library(tm)
```
读取RTF格式中的表格:
```{r}
# one table
doc <- docxtractr::read_docx('~/OneDrive/kintor/Daily_Work/KX826_US1003_PK/L16.2.4.1_ADSL_LREF06.docx')
```
```{r}
docx_tbl_count(doc)
```
```{r}
docx_extract_tbl(doc, 1)
```