這是我的可重現樣本:
set.seed(42)
n <- 1000
dat <- data.frame(Participant=1:20,
Environment=rep(LETTERS[1:2], n/2),
Condition=rep(LETTERS[25:26], n/2),
Gate= sample(1:5, n, replace=TRUE),
Block = sample(1:2, n, replace=TRUE),
Sound=rep(LETTERS[3:4], n/2),
Correct=sample(0:1, n, replace=TRUE)
)
從這個資料集中,我試圖在參與者級別進行分析,而不是專案級別。我試圖通過像這樣轉換資料集來實作這一點:
Participant_Data<- dat%>%
group_by(Condition, Gate, Sound, Participant) %>%
summarize(Accuracy = mean(Correct),
se = sd(Correct)/sqrt(length(Correct)))
然后我用這個新資料集制作一個圖表:
Participant_Data%>%
group_by(Condition, Gate, Sound) %>%
summarize(Proportion_Correct = mean(Accuracy),
standarderror = sd(Proportion_Correct)/sqrt(length(Proportion_Correct))) %>%
ggplot(aes(x = Gate, y = Proportion_Correct, color = Sound, group = Sound))
geom_line()
geom_errorbar(aes(ymin = Proportion_Correct - standarderror, ymax = Proportion_Correct standarderror), color = "Black", size = .15, width = .3)
geom_point(size = 2)
scale_y_continuous(labels = scales::percent)
facet_wrap(~Condition)
theme_minimal()
scale_color_brewer(palette = "Set1")
但正如您將看到的,我的錯誤值顯示為 NA,因此沒有顯示在我的圖表上。如果您能看到我沒有看到的內容,請告訴我,并提前致謝!
uj5u.com熱心網友回復:
正如@MrFlick 在評論中指出的那樣,問題在于sd(Proportion_Correct)您正在嘗試計算長度為 1 的向量的標準偏差,該向量將回傳NA.
相反,我建議計算標準誤差,因為sd(Accuracy, na.rm = TRUE)/sqrt(n())它看起來更像是計算標準誤差的自然方法,因為它Proportion_Correct被計算為mean(Accuracy).
library(dplyr)
library(ggplot2)
Participant_Data1 <- Participant_Data%>%
group_by(Condition, Gate, Sound) %>%
summarize(Proportion_Correct = mean(Accuracy),
standarderror = sd(Accuracy, na.rm = TRUE)/sqrt(n()))
#> `summarise()` has grouped output by 'Condition', 'Gate'. You can override using
#> the `.groups` argument.
ggplot(Participant_Data1, aes(x = Gate, y = Proportion_Correct, color = Sound, group = Sound))
geom_line()
geom_errorbar(aes(ymin = Proportion_Correct - standarderror, ymax = Proportion_Correct standarderror), color = "Black", size = .15, width = .3)
geom_point(size = 2)
scale_y_continuous(labels = scales::percent)
facet_wrap(~Condition)
theme_minimal()
scale_color_brewer(palette = "Set1")

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427620.html
