R 转换函数的形式';s结果并获得最大值
但是我希望这样的结果能够很容易地计算X平方R 转换函数的形式';s结果并获得最大值,r,function,chi-squared,R,Function,Chi Squared,但是我希望这样的结果能够很容易地计算X平方 [1] "18-24" vote3 ager 1 Gore 379 2 Bush 180 [1] "25-34" vote3 ager 1 Gore 728 2 Bush 793 [1] "35-44" vote3 ager 1 Gore 1213 2 Bush 1258 [1] "45-54" vote3 ager 1 Gore 1168 2 Bush 1004 [1] "55-64" vote3 ager
[1] "18-24"
vote3 ager
1 Gore 379
2 Bush 180
[1] "25-34"
vote3 ager
1 Gore 728
2 Bush 793
[1] "35-44"
vote3 ager
1 Gore 1213
2 Bush 1258
[1] "45-54"
vote3 ager
1 Gore 1168
2 Bush 1004
[1] "55-64"
vote3 ager
1 Gore 952
2 Bush 844
[1] "65+"
vote3 ager
1 Gore 1108
2 Bush 988
如何转变?
我成功地转变了。但我现在有一个新问题。。。。。。。。。
如何获得X平方的最大值
18-24 25-34 35-44 45-54 55-64 65+
1 Gore 379 728 1213 1168 952 1108
2 Bush 180 793 1258 1004 844 988
function您可以通过以下方式提取X平方统计信息:
functionA <- function(x)
{
for(i in 1:(ncol(x)-1))
{
for (j in (i+1):(ncol(x)))
{
n<-paste(colnames(x)[i], " vs ", colnames(x)[j], sep="")
k=chisq.test(cbind(as.numeric((x)[,i]),as.numeric((x)[,j])),correct = FALSE, simulate.p.value = TRUE, B = 9999)
s=k$statistic
print(n)
print(s)
}
}
}
而不仅仅是找到最大值。但是如果你的例子是可复制的,那么就更容易得到帮助。谢谢你的回答。但是我使用了函数(AGR)来计算结果。“Functional(ager)$统计信息”不起作用。“你知道怎么解决吗?”ZheZhang用一个最小且可重复的例子问了一个问题。为什么要发布图像而不是文本?在你的函数中,你没有返回任何东西;只是打印结果。由于您希望保存被测试的两个组的名称及其各自的X平方值,您应该为名称预先分配一个向量(VS=character(选择(6,2))
),为X-sq(Xsq=numeric(选择(6,2))
)分配一个向量,并在每次迭代中在各自的向量中保存“n”和“k”。顺便说一句,将您的函数设置为二进制(例如,myfun=function(x,y)setNames(chisq.test(x,y,…),粘贴(x,“VS”,y))
)并使用combn
将您的函数应用于列的所有组合应该更方便。@WernerHenze,因为它将是无序的……我不知道原因,太……@zhzhang请以文本形式发布文本,并阅读帮助页面了解如何设置文本格式。发布代码非常简单:粘贴、标记并选择“{}”图标。。。
18-24 25-34 35-44 45-54 55-64 65+
1 Gore 379 728 1213 1168 952 1108
2 Bush 180 793 1258 1004 844 988
functionA <- function(x)
{
for(i in 1:(ncol(x)-1))
{
for (j in (i+1):(ncol(x)))
{
n<-paste(colnames(x)[i], " vs ", colnames(x)[j], sep="")
k=chisq.test(cbind(as.numeric((x)[,i]),as.numeric((x)[,j])),correct = FALSE, simulate.p.value = TRUE, B = 9999)
s=k$statistic
print(n)
print(s)
}
}
}
chisq.test(c(89,37,30,28,2), c(40,20,20,15,5)) -> test
test$statistic
# X-squared
# 15