我對兩個資料框進行了 t 檢驗,并將結果存盤在兩個單獨的變數中。他們原來是串列。現在,我想制作一個包含 t 分數和 p 值的資料框,但我不知道該怎么做。我猜這些串列是 s3 類的。代碼。
AML_ttest <- apply(aml_df,1,t.test)
nrml_ttest <- apply(nrml_df,1,t.test)
運行AML_ttest[[9]]給出以下結果。
One Sample t-test
data: newX[, i]
t = 25.994, df = 25, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
6.612063 7.749997
sample estimates:
mean of x
7.18103
如何從每個串列元素中獲取 t 和 p 值?并制作一個新的資料框?
謝謝。
- -更新 - -
我嘗試了以下代碼。
# AML
AML_ttest <- apply(aml_df,1,t.test)
AML_ttest = do.call(rbind,AML_ttest)
res_AML_ttest <- AML_ttest[,c("statistic","p.value")]
# Normal
nrml_ttest <- apply(nrml_df,1,t.test)
nrml_ttest = do.call(rbind,nrml_ttest)
res_nrml_ttest <- nrml_ttest[,c("statistic","p.value")]
# Make df
df_ttest <- data.frame(res_AML_ttest, res_nrml_ttest)
df_ttest
# Output
statistic p.value statistic.1 p.value.1
1 56.71269 6.171562e-28 144.5161 1.569932e-52
2 75.79649 4.559861e-31 74.87025 5.317292e-42
3 17.68306 1.207297e-15 15.15478 1.891711e-17
4 108.4904 5.984139e-35 168.8557 4.993433e-55
5 152.8165 1.156183e-38 192.4672 3.959361e-57
6 63.21714 4.163004e-29 90.42468 5.112986e-45
這種方法好嗎?我可以走了嗎?
uj5u.com熱心網友回復:
我將使用此示例資料進行測驗:
TT1 <- apply(mtcars[1:3], 2, t.test)
我們可以看看str其中一個的結構,看看要尋找什么名字。
str(TT1[[1]])
# List of 10
# $ statistic : Named num 18.9
# ..- attr(*, "names")= chr "t"
# $ parameter : Named num 31
# ..- attr(*, "names")= chr "df"
# $ p.value : num 1.53e-18
# $ conf.int : num [1:2] 17.9 22.3
# ..- attr(*, "conf.level")= num 0.95
# $ estimate : Named num 20.1
# ..- attr(*, "names")= chr "mean of x"
# $ null.value : Named num 0
# ..- attr(*, "names")= chr "mean"
# $ stderr : num 1.07
# $ alternative: chr "two.sided"
# $ method : chr "One Sample t-test"
# $ data.name : chr "newX[, i]"
# - attr(*, "class")= chr "htest"
這表明我們可以使用"statistic"and "p.value"。直接的方法:
TT1[[1]][ c("statistic", "p.value") ]
# $statistic
# t
# 18.85693
# $p.value
# [1] 1.526151e-18
我們可以將它們收集在一起,例如:
out <- do.call(rbind.data.frame, lapply(TT1, `[`, c("statistic", "p.value")))
out
# statistic p.value
# mpg 18.85693 1.526151e-18
# cyl 19.59872 5.048147e-19
# disp 10.53069 9.189065e-12
wherempg等是行名。如果需要,它們可以被引入框架本身,
out$name <- rownames(out)
rownames(out) <- NULL # aesthetics only, not required
out
# statistic p.value name
# 1 18.85693 1.526151e-18 mpg
# 2 19.59872 5.048147e-19 cyl
# 3 10.53069 9.189065e-12 disp
(但是,由于您使用 t 檢驗串列,apply(MARGIN=1)如果原始資料沒有好的行名稱,它可能不會有意義地命名。)
uj5u.com熱心網友回復:
在您的示例中擁有更多資料通常會有所幫助,但在這種情況下,我們可以獲得 p 值等等。試試AML_ttest[[9]]$p.value。要查看所有選項,請嘗試str(AML_ttest[[9]])或str(summary(AML_ttest[[9]]))。
這個包broom真的很好。試試broom::t.test(MyData)。如果您使用 tidyverse 語法按某個變數對資料進行分組,您可以這樣做,例如MyData %>% group_by(ColumnA) %>% do(tidy(t.test(.$ColumnB, .$ColumnC, paired = TRUE))). (注意“.”)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/419226.html
標籤:
上一篇:Java-如何迭代哈希圖串列?
下一篇:在python中合并兩個元組串列
