我在python找到了一个解决方案,但是在寻找R中的解决方案。是否有一个等价于chi2.isf(p,df)的R?我知道chi等效的chi2.sf(p,df)是1-qchisq(p,df)。
答案 0 :(得分:2)
R中有一系列函数来处理卡方分布:dchisq(), pchisq(), qchisq(), rchisq()
。至于你的情况,你需要qchisq
来获得p值和自由度的卡方统计量:
qchisq(p = 0.01, df = 7)
要使用qchisq构建矩阵,我会做这样的事情。随意根据需要更改p值和自由度。
# Set p-values
p <- c(0.995, 0.99, 0.975, 0.95, 0.90, 0.10, 0.05, 0.025, 0.01, 0.005)
# Set degrees of freedom
df <- seq(1,20)
# Calculate a matrix of chisq statistics
m <- outer(p, df, function(x,y) qchisq(x,y))
# Transpose for a better view
m <- t(m)
# Set column and row names
colnames(m) <- p
rownames(m) <- df
m
0.995 0.99 0.975 0.95 0.9 0.1 0.05 0.025 0.01 0.005
1 7.879439 6.634897 5.023886 3.841459 2.705543 0.01579077 0.00393214 0.0009820691 0.0001570879 0.00003927042
2 10.596635 9.210340 7.377759 5.991465 4.605170 0.21072103 0.10258659 0.0506356160 0.0201006717 0.01002508365
3 12.838156 11.344867 9.348404 7.814728 6.251389 0.58437437 0.35184632 0.2157952826 0.1148318019 0.07172177459
4 14.860259 13.276704 11.143287 9.487729 7.779440 1.06362322 0.71072302 0.4844185571 0.2971094805 0.20698909350
5 16.749602 15.086272 12.832502 11.070498 9.236357 1.61030799 1.14547623 0.8312116135 0.5542980767 0.41174190383
6 18.547584 16.811894 14.449375 12.591587 10.644641 2.20413066 1.63538289 1.2373442458 0.8720903302 0.67572677746
7 20.277740 18.475307 16.012764 14.067140 12.017037 2.83310692 2.16734991 1.6898691807 1.2390423056 0.98925568313
8 21.954955 20.090235 17.534546 15.507313 13.361566 3.48953913 2.73263679 2.1797307473 1.6464973727 1.34441308701
9 23.589351 21.665994 19.022768 16.918978 14.683657 4.16815901 3.32511284 2.7003895000 2.0879007359 1.73493290500
10 25.188180 23.209251 20.483177 18.307038 15.987179 4.86518205 3.94029914 3.2469727802 2.5582121602 2.15585648130
11 26.756849 24.724970 21.920049 19.675138 17.275009 5.57778479 4.57481308 3.8157482522 3.0534841066 2.60322189052
12 28.299519 26.216967 23.336664 21.026070 18.549348 6.30379606 5.22602949 4.4037885070 3.5705689706 3.07382363809
13 29.819471 27.688250 24.735605 22.362032 19.811929 7.04150458 5.89186434 5.0087505118 4.1069154715 3.56503457973
14 31.319350 29.141238 26.118948 23.684791 21.064144 7.78953361 6.57063138 5.6287261030 4.6604250627 4.07467495740
15 32.801321 30.577914 27.488393 24.995790 22.307130 8.54675624 7.26094393 6.2621377950 5.2293488841 4.60091557173
16 34.267187 31.999927 28.845351 26.296228 23.541829 9.31223635 7.96164557 6.9076643535 5.8122124701 5.14220544304
17 35.718466 33.408664 30.191009 27.587112 24.769035 10.08518633 8.67176020 7.5641864496 6.4077597777 5.69721710150
18 37.156451 34.805306 31.526378 28.869299 25.989423 10.86493612 9.39045508 8.2307461948 7.0149109012 6.26480468451
19 38.582257 36.190869 32.852327 30.143527 27.203571 11.65091003 10.11701306 8.9065164820 7.6327296476 6.84397144548
20 39.996846 37.566235 34.169607 31.410433 28.411981 12.44260921 10.85081139 9.5907773923 8.2603983325 7.43384426293
答案 1 :(得分:1)
如果要创建列联表,可以使用函数table()
。
使用此功能,您将获得此类型的表格:
table(var1,var2)
var1$1 var1$2
var2$1 n n
var2$2 n n
其中$ 1和$ 2是变量1和2的模态。
如果您有包含变量的数据框,则必须将此函数设置为此table(namedf$var1, namedf$var2)
。
如果你想进行卡方检验,函数是chisq.test()
chisq.test(namedf$var1, namedf$var2, correct=FALSE)