在data.frame上执行ttest

时间:2017-08-30 19:57:16

标签: r dataframe lapply t-test

尝试从data.frame执行ttest(并获得p.value),其中一列包含组(好与坏),其余列是数字。

我在这里制作了一个玩具数据集:

W <- rep(letters[seq( from = 1, to = 2)], 25)
X <- rnorm(n=50, mean = 10, sd = 5)
Y <- rnorm(n=50, mean = 15, sd = 6)
Z <- rnorm(n=50, mean = 20, sd = 5)
test_data <- data.frame(W, X, Y, Z)

然后我将数据转换为长格式:

melt_testdata <- melt(test_data)

并执行了t.test

lapply(unique(melt_testdata$variable),function(x){
  Good <- subset(melt_testdata, W  == 'a' & variable ==x)$variable
  Bad <- subset(melt_testdata, W == 'b' & variable ==x)$variable
  t.test(Good,Bad)$p.value
})

但是我没有得到t.test结果,而是收到以下错误消息:

Error in if (stderr < 10 * .Machine$double.eps * max(abs(mx), abs(my))) stop("data are essentially constant") : 
  missing value where TRUE/FALSE needed In addition: Warning messages:
1: In mean.default(x) : argument is not numeric or logical: returning NA
2: In var(x) :
  Calling var(x) on a factor x is deprecated and will become an error.
  Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.
3: In mean.default(y) : argument is not numeric or logical: returning NA
4: In var(y) :
  Calling var(x) on a factor x is deprecated and will become an error.
  Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.

然后我尝试写循环(第一次......)

good <- matrix(,50)
bad <- matrix(,50)
cnt=3
out <- rep(0,cnt)


for (i in 2:4){
  good[i] <- subset(test_data, W == 'a', select= test_data[,i])
  bad[i] <- subset(test_data, W == 'b', select= test_data[,i])
  out[i] <- print(t.test(good[[i]], bad[[i]])$p.value)
}

仍未获得p.values ....... 这是错误消息

Error in x[j] : only 0's may be mixed with negative subscripts

感谢任何方法的帮助,谢谢!

2 个答案:

答案 0 :(得分:2)

I think you'll have better luck with the function checkWin() { let emptyword = ["h", "e", "l", "l", "o"] let computerword = "hello"; var a = emptyword.join(""); let b = computerword; console.log("computerword: " + b); console.log("emptyword is: " + a); if (a === b) { console.log("someone has won"); } else if (a !== b) { console.log("b is not same as c"); } } checkWin() method of let emptyword = ["h,", "e,", "l,", "l", "o"] . Try

let emptyword = ["h", "e", "l", "l", "o"]

Edit:

With stricter adherence to the formula philosophy, you can use the following: which is actually a bit cleaner looking.

t.test

答案 1 :(得分:0)

Here is a solution using <a href="https://link-to-whatever.com/looser"> <!-- If diced < 6 --> and the formula argument to >>>import statsmodels.stats.power as smp >>>n_levels_variable_a = 2 >>>n_levels_variable_b = 3 >>>smp.GofChisquarePower().solve_power(0.346, power=.8, n_bins=(n_levels_variable_a-1)*(n_levels_variable_b-1), alpha=0.05) . <TouchableOpacity style={Styles.oneButton}> <View style={Styles.oneButtonView}> <Text style={Styles.payTextDollar}> <Image style={Styles.imageButton} source={props.image} /> Pay </Text> </View> </TouchableOpacity> oneButton: { height: 60, backgroundColor: '#e41c23', } oneButtonView: { flex: 1, flexDirection: 'row', alignItems: 'center', height: 20, justifyContent: 'center', } payTextDollar: { flex: 1, color: '#ffffff', fontSize: 20, fontFamily: 'MetaOffc-Light', textAlign: 'center', } imageButton: { marginTop: 6, } works on each group defined by the dplyr t.test extracts values from the do output and makes them into a group_by..

glance