How to extract the dataset from an “htest” object when using formula in r

纵然是瞬间 提交于 2020-04-30 07:18:05

问题


The question is how to extract the dataset from an "htest" object when using formula. For example,

library(gginference)
t_test <- t.test(formula = pulse~ gender,  
             data=questionnaire)
t_test$data.name

returns

[1] "pulse by gender"

Is there a way to extract the dataset (in this case "questionnaire")?


回答1:


There is no way that you could extract the data from the list of the output of t.test(). The code for the components of the output of t.test() is this:

rval <- list(statistic = tstat, parameter = df, p.value = pval,
           conf.int = cint, estimate = estimate, null.value = mu,
           alternative = alternative,
           method = method, data.name = dname)
    class(rval) <- "htest"

You can see that the list of the output of t.test() does not include the variables.




回答2:


Mohanasundaram answers the OP's specific question. t.test doesn't store your data in the output so you can't retrieve it.

However, via the OP's comment, if the goal is to use this in ggttest to visualize, there are two options.

First, ggttest is expecting the that you didn't use the formula syntax, and instead you directly subset your dataframe (via $). This is because it expects the t_test$data.name output to look something like this: "questionnaire$pulse by questionnaire$gender" The quickest approach would be to not use formulas, if possible:

library(gginference)

t_test <- t.test(questionnaire$pulse ~ questionnaire$gender)

ggttest(t_test) #should give correct output

If for some reason you MUST use a formula, you can manually change t_test$data.name to match what ggttest is expecting:

t_test <- t.test(formula = pulse~ gender,  
             data=questionnaire)

t_test$data.name <- "questionnaire$pulse by questionnaire$gender"

ggttest(t_test)


来源:https://stackoverflow.com/questions/61434785/how-to-extract-the-dataset-from-an-htest-object-when-using-formula-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!