Chi-squared test of independence on all combinations of columns in a dataframe in R

两盒软妹~` 提交于 2019-12-03 16:36:05

You need the combn function to find all the combinations of the columns and then apply them to your function, something like this:

apply(combn(1:ncol(plots), 2), 2, function(ind) CHI(plots[, ind[1]], plots[, ind[2]]))

I think you are looking for something like this. I used the iris dataset.

require(datasets)
ind<-combn(NCOL(iris),2)
lapply(1:NCOL(ind), function (i) CHI(iris[,ind[1,i]],iris[,ind[2,i]]))

The below R code run chisquare test for every categorical variable / every factor of a r dataframe, against a variable given (x or y chisquare parameter is kept stable, is explicitly defined):

Define your variable Please - change df$variable1 to your desired factor variable and df to your desirable dataframe that contain all the factor variables tested against the given df$variable1

Define your Dataframe A new dataframe is created (df2) that will contain all the chi square values / dfs, p value of the given variable vs dataframe comparisons

Code created / completed/ altered from similar posts in stackoverflow, neither that produced my desired outcome. Chi-Square Tables statistic / df / p value for variable vs dataframe "2" parameter define column wide comparisons - check apply (MARGIN) option.

df2 <- t(round(cbind(apply(df, 2, function(x) {
  ch <- chisq.test(df$variable1, x)
  c(unname(ch$statistic), ch$parameter, ch$p.value )})), 3))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!