-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamine.qmd
More file actions
438 lines (314 loc) · 11.8 KB
/
Copy pathexamine.qmd
File metadata and controls
438 lines (314 loc) · 11.8 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
---
title: "Examine results"
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
echo = TRUE,
eval = FALSE
)
```
```{r}
#| label: setup
#| echo: false
#| message: false
#| eval: true
library(glue)
library(knitr)
library(Pmetrics)
r_help <- function(pkg, name, fn = NULL) {
if(is.null(fn)) {fn <- name}
glue::glue("[`{name}`](https://rdrr.io/pkg/{pkg}/man/{fn}.html)")
}
gh_help <- function(name) {
glue::glue("[`{name}`](https://lapkb.github.io/Pmetrics/reference/{name}.html)")
}
pmetrics <- function(){
knitr::asis_output("[Pmetrics]{style=\"color: #841010; font-family: 'Arial', Arial, sans-serif; font-weight: 900;\"}")
}
run1 <- NPex
```
Recall that in the previous exercises, we loaded our run results with the following.
```{r}
run1 <- PM_load(1, path = "Runs")
```
`run1` is now a `r gh_help("PM_result")` R6 object that contains all the results from the NPAG run. Let's dig into it in more detail.
## Objects within a PM_result
### Data
#### Data plots
You can plot the original data using R6 with various options. Type `?plot.PM_data` in the R console for help.
```{r}
#| eval: false
run1$data$plot()
```
```{r}
#| eval: true
#| echo: false
plotly::plotly_build(run1$data$plot())
```
Here's some nice grouping and coloring. If you use grouping, provide names in `group_names` and colors/symbols for each group as arguments to `marker`. Colors of lines will be set to the same as for `marker` to avoid a mess of colors.
```{r}
#| eval: false
run1$data$plot(group = "gender", group_names = c("Female", "Male"),
marker = list(color = c("red","blue"), symbol = c("circle","triangle-up")))
```
```{r}
#| eval: true
#| echo: false
plotly::plotly_build(run1$data$plot(group = "gender", group_names = c("Female", "Male"),
marker = list(color = c("red","blue"), symbol = c("circle","triangle-up"))))
```
The next one splits the data by subject but will not render easily in this document. You can interact with it in your own script or Quarto document.
```{r}
#| eval: false
run1$data$plot(overlay = FALSE, xlim = c(119, 145))
```
The following are the older S3 method with `plot(...)` for the first two examples. You can use R6 or S3 for any Pmetrics object. We will focus on R6 as the more modern way.
```{r}
#| eval: false
plot(run1$data)
```
If you would like to plot the observations and posterior predictions made at `idelta` intervals (see options for `$fit` method in `r gh_help("PM_model")` , you can do that by adding a `pred` line to a data plot. See `r gh_help("plot.PM_data")` for more information.
```{r}
#| eval: false
# suppress the segments joining observations and add posterior predictions with `pred = "post"`
run1$data$plot(line = list(join = FALSE, pred = "post"), xlim = c(119, 145))
```
Typically you would want to use `overlay = FALSE` with the above to avoid a mess of lines joining observations and predictions from different subjects, but it will not render easily in this document. You can interact with it in your own script or Quarto document.
```{r}
#| eval: true
#| echo: false
# suppress the segments joining observations and add posterior predictions with `pred = "post"`
plotly::plotly_build(run1$data$plot(line = list(join = FALSE, pred = "post"), xlim = c(119, 145)))
```
### Observed vs. Predicted (OP)
#### OP plots
You can plot observed vs. predicted data. Type `?plot.PM_op` in the R console for help or check out `r gh_help("plot.PM_op")`.
```{r}
#| eval: false
run1$op$plot()
```
```{r}
#| eval: true
#| echo: false
plotly::plotly_build(run1$op$plot())
```
Below, you can see how to change the prediction type, regression line and color, and plotting symbol and color. Most plots in Pmetrics have `marker` and `line` list arguments to customize the appearance of the plotting symbols and lines.
```{r}
#| eval: false
run1$op$plot(pred.type = "pop")
run1$op$plot(line = list(lm = FALSE, loess = list(color = "red")), marker = list(symbol = 3, color = "green"))
```
```{r}
#| label: op_plots_options
#| eval: true
#| echo: false
plotly::plotly_build(run1$op$plot(pred.type = "pop"))
plotly::plotly_build(run1$op$plot(line = list(lm = FALSE, loess = list(color = "red")), marker = list(symbol = 3, color = "green")))
```
#### OP summaries
Get a summary with bias and imprecision of the population predictions; `?summary.PM_op` in the console or `r gh_help("summary.PM_op")` for more information. The default `pred.type` is "post", which are the predictions based on the full Bayesian posterior distribution of parameter values for an individual. The predictions are the weighted mean or median (controlled by the `icen` argument in all `r pmetrics()` functions) of the predictions from each support point in the individual's Bayesian posterior, joint probability parameter distribution, i.e. the support points in the posterior. Here we will look at the population predictions based on the means of the parameter values.
```{r}
#| eval: false
run1$op$summary(pred.type = "pop", icen = "mean")
```
The nicely formatted table below will open in your Viewer panel giving you the summary statistics for non-missing observations and corresponding predictions.

Notes at the bottom of the table tell you the basis for the predictions, which can be changed with arguments to the `$summary()` method, which calls `r gh_help("summary.PM_op")`. For example, aince neither "pop" nor "mean" are the defaults, we have to specify them. We can also use the S3 method for the same summary below.
```{r}
#| label: S3_op_summary
summary(run1$op, pred.type = "pop", icen = "mean") # S3 method
```
As with any object in `r pmetrics()`, the `PM_op` R6 object has fields and methods. As a reminder of what we've covered in several previous chapters, R6 fields contain data and methods are functions that act on the data. All fields and methods are accessed with `$` added to the name of the object. For example, in a `PM_op` object and most other `r pmetrics()` objects, the **raw data** can be accessed via the `$data` field.
```{r}
run1$op$data
```
Having the raw data provides a data frame compatible with any R function, particularly those from [**Tidyverse**](https://www.tidyverse.org), including piping (`|>`). If you're not familiar with the pipe operator, see [this article](https://magrittr.tidyverse.org/). We strongly recommend Tidvyerse for data manipulation and visualization in R, and `r pmetrics()` works well with it. See [R for Data Science](https://r4ds.hadley.nz) for more information.
This allows pre-processing in ways more flexible than the default plot method.
```{r}
library(tidyverse)
run1$op$data |> plot()
run1$op$data |>
filter(pred > 5) |>
filter(pred < 10) |>
summary()
```
See a header with the first 10 rows of the op object:
```{r}
head(run1$op$data, 10)
```
### Final population joint density
#### Final plots
Plot the final population joint density. Type `?plot.PM_final` in the R console for help.
```{r}
run1$final$plot()
# alternate methods with same result
run1$final$data |> plot() # tidyverse style
plot(run1$final) # S3 method
plot(run1$final$data) # S3 method
```
```{r}
#| eval: true
#| echo: false
plotly::plotly_build(run1$final$plot())
```
You can add a kernel density curve.
```{r}
run1$final$plot(line = list(color = "red"))
```
```{r}
#| eval: true
#| echo: false
plotly::plotly_build(
run1$final$plot(line = list(color = "red")))
```
It is possible to make a bivariate plot. Plotting formulae in R are of the form `y~x`
```{r}
run1$final$plot(ke ~ v,
marker = list(color = "red", symbol = "diamond"))
```
```{r}
#| eval: true
#| echo: false
plotly::plotly_build(
run1$final$plot(ke ~ v,
marker = list(color = "red", symbol = "diamond")))
```
#### Final summaries
See a summary with confidence intervals around the medians and the Median Absolute Weighted Difference (MAWD); `?summary.PM_final` for help and further explanation.
```{r}
run1$final$summary()
```
The original final object can be accessed with the `$data` field.
```{r}
run1$final$data
names(run1$final$data)
```
See the population points:
```{r}
#| eval: true
run1$final$popPoints
```
Or drill down one more level into the raw data. The `$final$popPoints` field above pulls its information from the `$final$data$popPoints` field below, but is provided as a simpler way to access the population points.
```{r}
run1$final$data$popPoints
```
See the population mean parameter values:
```{r}
#| eval: true
run1$final$popMean
```
### Cycle information
#### Cycle plots
Plot cycle information; type `?plot.PM_cycle` in the R console for help.
```{r}
run1$cycle$plot()
```
```{r}
#| eval: true
#| echo: false
plotly::plotly_build(
run1$cycle$plot())
```
#### Cycle summaries
Summarize the cycle information; `?summary.PM_cycle` for help.
```{r}
run1$cycle$summary()
# alternatives
run1$cycle$data |> summary() # tidyverse style
summary(run1$cycle) # S3 method
summary(run1$cycle$data) # S3 method
```
```{r}
#| eval: true
#| echo: false
run1$cycle$summary()
```
### Covariate information
#### Covariate plots
Plot covariate information; type `?plot.PM_cov` in the R console for help. Recall that plotting formulae in R are of the form `y~x`.
```{r}
run1$cov$plot(v ~ wt)
```
```{r}
#| eval: true
#| echo: false
plotly::plotly_build(
run1$cov$plot(v ~ wt))
```
You can also use the raw data in the `$data` field to make plots with other R functions, including those from **Tidyverse**. Here we will filter the data for age \> 25 and then plot volume vs. weight.
```{r}
run1$cov$data |>
filter(age > 25) |>
plot(v ~ wt)
```
```{r}
#| eval: false
#| echo: false
run1$cov$data |>
dplyr::filter(age > 25) |>
plot(v ~ wt)
```
Change the formatting of the plot with the `line` and `marker` list arguments.
```{r}
run1$cov$plot(ke ~ age, line = list(loess = FALSE, lm = list(color = "red")),
marker = list(symbol = 3))
```
```{r}
#| eval: true
#| echo: false
plotly::plotly_build(
run1$cov$plot(ke ~ age, line = list(loess = FALSE, lm = list(color = "red")),
marker = list(symbol = 3)))
```
Another plot with mean Bayesian posterior parameter and covariate values...remember the `icen` argument?
```{r}
run1$cov$plot(v ~ wt, icen = "mean")
```
```{r}
#| eval: true
#| echo: false
plotly::plotly_build(run1$cov$plot(v ~ wt, icen = "mean"))
```
When `time` is the `x` variable, the `y` variable is aggregated by subject. In R plot formulae, calculations on the fly can be included using the `I()` function.
```{r}
run1$cov$plot(I(v * wt) ~ time)
```
```{r}
#| eval: true
#| echo: false
plotly::plotly_build(
run1$cov$plot(I(v * wt) ~ time))
```
The above plots the product of volume and weight vs. time for each subject.
#### Covariate summaries
Just as you have the option to plot by mean covariate and posterior parameter values, you can summarize with means; `?summary.PM_cov` for help.
```{r}
#| eval: true
run1$cov$summary(icen = "mean")
```
The `run1$cov` object looks like a data frame, but is really an R6 `PM_cov` object whose `$print()` method returns a data frame. You can access the raw data with the `$data` field, which is truly a data frame.
```{r}
run1$cov$data[, 1:3] # for example
run1$cov$data |> filter(gender == 1) |> summary() # using Tidyverse
```
This returns a data frame with the means of the individual's covariates over the observation period and the mean of the Bayesian posterior parameter values.
When trying to find covariate-parameter relationships, it is convenient to examine at all possible covariate-parameter relationships by multiple linear regression with forward and backward elimination - type `?PM_step` in the R console for help or see `r gh_help("PM_step")`.
```{r}
#| eval: true
run1$step()
```
...or on the cov object directly.
```{r}
run1$cov$step()
```
`icen` works here too.
```{r}
run1$step(icen = "mean")
```
If you wish to see P values for forward elimination only:
```{r}
#| eval: true
run1$step(direction = "forward")
```