gpt4 book ai didi

r - 如何正确使用 ggstatsplot 包中的 grouped_ggwithinstats() 函数

转载 作者:行者123 更新时间:2023-12-05 05:30:12 26 4
gpt4 key购买 nike

我正在尝试使用 grouped_ggwithinstats()来自 ggstatsplot 的函数包裹:

我有这个数据集:

df <- structure(list(time = c(0L, 1L, 2L, 3L, 0L, 1L, 2L, 3L, 0L, 1L, 
2L, 3L, 0L, 1L, 2L, 3L), group1 = c("A", "A", "A", "A", "B",
"B", "B", "B", "C 1", "C 1", "C 1", "C 1", "C 2", "C 2", "C 2",
"C 2"), group2 = c("Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z",
"Z", "Z", "Z", "Z", "Z", "Z", "Z"), value = c(100L, 60L, 30L,
32L, 100L, 2L, 3L, 1L, 100L, 17L, 17L, 8L, 100L, 35L, 36L, 22L
)), class = "data.frame", row.names = c("1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"))

time group1 group2 value
1 0 A Z 100
2 1 A Z 60
3 2 A Z 30
4 3 A Z 32
5 0 B Z 100
6 1 B Z 2
7 2 B Z 3
8 3 B Z 1
9 0 C 1 Z 100
10 1 C 1 Z 17
11 2 C 1 Z 17
12 3 C 1 Z 8
13 0 C 2 Z 100
14 1 C 2 Z 35
15 2 C 2 Z 36
16 3 C 2 Z 22

使用这段代码:

library(ggstatsplot)

ggbetweenstats(
data = df,
x = time,
y = value,
type = "nonparametric",
plot.type = "box",
centrality.plotting = FALSE
)

enter image description here我现在想按 group1 分组并用每组标记点:所以当我这样做时:

grouped_ggbetweenstats(
data = df,
x = time,
y = value,
grouping.var = group1,
type = "nonparametric",
plot.type = "box",
centrality.plotting = FALSE
)

enter image description here

我不明白 ggwithinstats 之间的区别与 ggbetweenstats关于 grouped_ggbetweenstats()grouped_ggwithinstats()后者给出错误:

Error in data.frame(..., check.names = FALSE) : 
arguments imply differing number of rows: 0, 1

如果时间点 0、1、2、3 的变化在每个组中都显着,我尝试使用重复方差分析来测试。例如在 A 组、B 组、C 组 1、C 组 2。

最佳答案

您正确使用了 grouped_ggwithinstats(),但您收到了所有这些警告,因为没有足够的数据来运行假设检验和估计。

每组每个条件都有一个数据,因此无法运行统计测试,因此副标题和说明是空的。

library(dplyr, warn.conflicts = FALSE)
library(ggstatsplot)
#> You can cite this package as:
#> Patil, I. (2021). Visualizations with statistical details: The 'ggstatsplot' approach.
#> Journal of Open Source Software, 6(61), 3167, doi:10.21105/joss.03167

df <- tibble(time = c(
0L, 1L, 2L, 3L, 0L, 1L, 2L, 3L, 0L, 1L,
2L, 3L, 0L, 1L, 2L, 3L
), group1 = c(
"A", "A", "A", "A", "B",
"B", "B", "B", "C 1", "C 1", "C 1", "C 1", "C 2", "C 2", "C 2",
"C 2"
), group2 = c(
"Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z",
"Z", "Z", "Z", "Z", "Z", "Z", "Z"
), value = c(
100L, 60L, 30L,
32L, 100L, 2L, 3L, 1L, 100L, 17L, 17L, 8L, 100L, 35L, 36L, 22L
))

df %>%
group_by(group1, time) %>%
count()
#> # A tibble: 16 × 3
#> # Groups: group1, time [16]
#> group1 time n
#> <chr> <int> <int>
#> 1 A 0 1
#> 2 A 1 1
#> 3 A 2 1
#> 4 A 3 1
#> 5 B 0 1
#> 6 B 1 1
#> 7 B 2 1
#> 8 B 3 1
#> 9 C 1 0 1
#> 10 C 1 1 1
#> 11 C 1 2 1
#> 12 C 1 3 1
#> 13 C 2 0 1
#> 14 C 2 1 1
#> 15 C 2 2 1
#> 16 C 2 3 1

grouped_ggwithinstats(
data = df,
x = time,
y = value,
grouping.var = group1,
type = "nonparametric",
pairwise.comparisons = FALSE,
centrality.plotting = FALSE
)
#> Warning: Groups with fewer than two data points have been dropped.
#> Groups with fewer than two data points have been dropped.
#> Groups with fewer than two data points have been dropped.
#> Groups with fewer than two data points have been dropped.
#> Warning in max(data$density): no non-missing arguments to max; returning -Inf
#> Warning: Computation failed in `stat_ydensity()`
#> Caused by error in `$<-.data.frame`:
#> ! replacement has 1 row, data has 0
#> Warning: Groups with fewer than two data points have been dropped.
#> Groups with fewer than two data points have been dropped.
#> Groups with fewer than two data points have been dropped.
#> Groups with fewer than two data points have been dropped.
#> Warning in max(data$density): no non-missing arguments to max; returning -Inf
#> Warning: Computation failed in `stat_ydensity()`
#> Caused by error in `$<-.data.frame`:
#> ! replacement has 1 row, data has 0
#> Warning: Groups with fewer than two data points have been dropped.
#> Groups with fewer than two data points have been dropped.
#> Groups with fewer than two data points have been dropped.
#> Groups with fewer than two data points have been dropped.
#> Warning in max(data$density): no non-missing arguments to max; returning -Inf
#> Warning: Computation failed in `stat_ydensity()`
#> Caused by error in `$<-.data.frame`:
#> ! replacement has 1 row, data has 0
#> Warning: Groups with fewer than two data points have been dropped.
#> Groups with fewer than two data points have been dropped.
#> Groups with fewer than two data points have been dropped.
#> Groups with fewer than two data points have been dropped.
#> Warning in max(data$density): no non-missing arguments to max; returning -Inf
#> Warning: Computation failed in `stat_ydensity()`
#> Caused by error in `$<-.data.frame`:
#> ! replacement has 1 row, data has 0

创建于 2022-12-12 reprex v2.0.2

关于r - 如何正确使用 ggstatsplot 包中的 grouped_ggwithinstats() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74762091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com