Skip to content

Commit

Permalink
update vignettes
Browse files Browse the repository at this point in the history
  • Loading branch information
max-pilz committed Nov 22, 2023
1 parent 483fe48 commit 0ba9e54
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
20 changes: 10 additions & 10 deletions vignettes/a_usage_guide.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ dataset for instructive purposes:
library(dplyr, warn.conflicts = FALSE)
library(forcats)
set.seed(123)
dat <- iris[, c("Species", "Sepal.Length")] %>%
mutate(animal = c("Mammal", "Fish") %>% rep(75) %>% factor()) %>%
mutate(food = c("fries", "wedges") %>% sample(150, TRUE) %>% factor())
dat <- iris[, c("Species", "Sepal.Length")] |>
mutate(animal = c("Mammal", "Fish") |> rep(75) |> factor()) |>
mutate(food = c("fries", "wedges") |> sample(150, TRUE) |> factor())
head(dat)
```

Expand Down Expand Up @@ -91,7 +91,7 @@ Rstudios autocomplete suggestions are very helpful when navigating this list.
The `print` function returns a formatted version of this list, which you can also save and access using the same syntax.

```{r}
my_table <- descr(dat) %>% print(silent=TRUE)
my_table <- descr(dat) |> print(silent=TRUE)
```


Expand Down Expand Up @@ -130,22 +130,22 @@ There are a lot of different tests available. Check out the test_choice vignette
Here are some different tests in action:

```{r}
descr(dat %>% select(-"Species"), "animal", test_options = list(exact=TRUE, nonparametric=TRUE))
descr(dat |> select(-"Species"), "animal", test_options = list(exact=TRUE, nonparametric=TRUE))
```


```{r}
descr(dat %>% select(c("Species", "Sepal.Length")), "Species", test_options = list(nonparametric=TRUE))
descr(dat |> select(c("Species", "Sepal.Length")), "Species", test_options = list(nonparametric=TRUE))
```

## Paired observations

In situations with paired data, the `group` variable usually denotes the timing of the measurement (e.g. "before" and "after" or "time 1", "time 2", etc.). In these scenarios, you need an additional index variable that specifies which observations from the different timepoints should be paired. The `test_options =list(paired=TRUE, indices = <Character name of index variable name or vector of indices>)` option can be used to specify the pairing indices, see the example below. DescrTab2 only works with data in "long format", see e.g. `?reshape` or `?tidyr::pivot_longer` for information on how to transoform your data from wide to long format.

```{r}
descr(dat %>% mutate(animal = fct_recode(animal, Before="Fish", After="Mammal")) %>% select(-"Species"), "animal", test_options = list(paired=TRUE, indices=rep(1:75, each=2)))
descr(dat |> mutate(animal = fct_recode(animal, Before="Fish", After="Mammal")) |> select(-"Species"), "animal", test_options = list(paired=TRUE, indices=rep(1:75, each=2)))
descr(dat %>% mutate(animal = fct_recode(animal, Before="Fish", After="Mammal"), idx = rep(1:75, each=2)) %>% select(-"Species"), "animal", test_options = list(paired=TRUE, indices="idx" ))
descr(dat |> mutate(animal = fct_recode(animal, Before="Fish", After="Mammal"), idx = rep(1:75, each=2)) |> select(-"Species"), "animal", test_options = list(paired=TRUE, indices="idx" ))
```

## Significant digits
Expand Down Expand Up @@ -173,7 +173,7 @@ Let's say you have a categorical variable, but for some reason it's levels are n
```{r}
# Create example dataset
dat2 <- iris
dat2$cat_var <- c(1,2) %>% sample(150, TRUE) %>% factor()
dat2$cat_var <- c(1,2) |> sample(150, TRUE) |> factor()
dat2 <- dat2[, c("Species", "cat_var")]
descr(dat2, "Species", summary_stats_cat=list(mean=DescrTab2:::.factormean))
Expand Down Expand Up @@ -234,7 +234,7 @@ custom_ttest <- list(
}
)
descr(iris %>% select(-Species), test_options = list(test_override = custom_ttest))
descr(iris |> select(-Species), test_options = list(test_override = custom_ttest))
```

## Supress the last factor level
Expand Down
23 changes: 11 additions & 12 deletions vignettes/c_other_software_comparison.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ knitr::opts_chunk$set(
comment = "#>"
)
library(DescrTab2)
library(tidyverse)
options(print_format="html")
```

Expand Down Expand Up @@ -48,7 +47,7 @@ Dataset origin: `?wilcox.test` (accessed on R-version 4.0.3).
x <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30)
y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29)
dat_wilcox.test_1_sample <- tibble(diff = x-y)
dat_wilcox.test_1_sample <- tibble::tibble(diff = x-y)
descr(dat_wilcox.test_1_sample, test_options = c(nonparametric=TRUE))
```
Expand Down Expand Up @@ -77,7 +76,7 @@ Dataset origin: `?wilcox.test` (accessed on R-version 4.0.3).
x <- c(0.80, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46)
y <- c(1.15, 0.88, 0.90, 0.74, 1.21)
group <- c(rep("Trt", length(x)), rep("Ctrl", length(y)))
dat_wilcox.test_2_sample <- tibble(var=c(x,y), group=group)
dat_wilcox.test_2_sample <- tibble::tibble(var=c(x,y), group=group)
descr(dat_wilcox.test_2_sample, "group", test_options = c(nonparametric=TRUE))
```
Expand Down Expand Up @@ -108,7 +107,7 @@ x <- c(2.9, 3.0, 2.5, 2.6, 3.2) # normal subjects
y <- c(3.8, 2.7, 4.0, 2.4) # with obstructive airway disease
z <- c(2.8, 3.4, 3.7, 2.2, 2.0) # with asbestosis
group <- c(rep("Trt", length(x)), rep("Ctrl", length(y)), rep("Placebo", length(z)))
dat_kruskal.test <- tibble(var=c(x,y,z), group=group)
dat_kruskal.test <- tibble::tibble(var=c(x,y,z), group=group)
descr(dat_kruskal.test, "group", test_options = c(nonparametric=TRUE))
```
Expand Down Expand Up @@ -165,7 +164,7 @@ matrix(c(5.40, 5.50, 5.55,
c("Round Out", "Narrow Angle", "Wide Angle")))
idx <- rep(1:22, 3)
dat <- tibble(var = c(RoundingTimes[,1], RoundingTimes[,2], RoundingTimes[,3]),
dat <- tibble::tibble(var = c(RoundingTimes[,1], RoundingTimes[,2], RoundingTimes[,3]),
group = c(rep("Round Out", 22), rep("Narrow Angle", 22), rep("Wide Angle", 22)))
Expand Down Expand Up @@ -203,7 +202,7 @@ d.frm <- DescTools::Untable(xtabs(c(6,2,2,6,16,4,4,6) ~ .,
d.long <- reshape(d.frm, varying=1:3, times=names(d.frm)[c(1:3)],
v.names="resp", direction="long")
idx <- d.long$id
dat <- d.long[, 1:2] %>% mutate(time=as.character(time), resp=as.character(resp))
dat <- d.long[, 1:2] |> dplyr::mutate(time=as.character(time), resp=as.character(resp))
descr(dat, "time", test_options = list(indices=idx, paired=TRUE))
Expand Down Expand Up @@ -281,7 +280,7 @@ Dataset origin: "Gender-Party dataset":`?chisq.test` (accessed on R-version 4.0.

```{r chisq.test1, results='asis'}
dat <- tibble(gender=c(rep("F",sum(c(762, 327, 468)) ), rep("M", sum( c(484, 239, 477)))),
dat <- tibble::tibble(gender=c(rep("F",sum(c(762, 327, 468)) ), rep("M", sum( c(484, 239, 477)))),
party=c(rep("Democrat", 762), rep("Independent", 327), rep("Republican", 468),
rep("Democrat", 484), rep("Independent", 239), rep("Republican", 477)))
Expand All @@ -296,7 +295,7 @@ chisq.test(table(dat$party))
```

```{r chisq.test3, results='asis'}
dat <- tibble(
dat <- tibble::tibble(
a = factor(c(0,
0,
Expand Down Expand Up @@ -419,9 +418,9 @@ dat <- nlme::Orthodont
dat2 <- nlme::Orthodont[1:64,]
dat2$Sex <- "Divers"
dat2$distance <- dat2$distance + c(rep(0.1*c(1,4,3,2), 10), 0.1*rep(c(0.4,2,1.5, 2.3), 6) )
dat2$Subject <- str_replace_all(dat2$Subject, "M", "D")
dat <- bind_rows(dat, dat2)
dat <- as_tibble(dat)
dat2$Subject <- stringr::str_replace_all(dat2$Subject, "M", "D")
dat <- dplyr::bind_rows(dat, dat2)
dat <- tibble::as_tibble(dat)
descr(dat[, c("Sex", "distance")], "Sex", test_options = list(paired=TRUE, indices=dat$Subject))
Expand Down Expand Up @@ -450,7 +449,7 @@ Dataset origin: selfmade.

DescrTab2 uses the exact2x2::boschloo with option `tsmethod=central` to calculate p-values. There is no comparison for this option readily available.
```{r boschloo1, results='asis'}
dat <- tibble(gender=factor(c("M", "M", "M", "M", "M", "M", "F", "F", "F", "F", "F")),
dat <- tibble::tibble(gender=factor(c("M", "M", "M", "M", "M", "M", "F", "F", "F", "F", "F")),
party=factor(c("A", "A", "B", "B", "B", "B", "A", "A", "A", "B", "B")))
descr(dat, "gender", test_options = c(exact=TRUE))
```
Expand Down

0 comments on commit 0ba9e54

Please sign in to comment.