在R中复制SAS加权频率和chisq-test

时间:2015-11-05 13:47:28

标签: r weighted

我试图在R中复制SAS频率计数和chisq-test。

sas数据和代码:

DATA test; 
   INPUT sex $ group weight ; 
   DATALINES; 
F  1  0.8 
M  1  0.9
F  1  1.0
M  1  1.1
F  1  1.2
M  1  1.3
F  2  1.4
M  2  1.5
F  2  1.6
M  2  1.7
F  1  0.8 
M  1  0.9
F  1  1.0
M  1  1.1
F  1  1.2
M  1  1.3
F  2  1.4
M  2  1.5
F  2  1.6
M  2  1.7
F  1  0.8 
M  1  0.9
F  1  1.0
M  1  1.1
F  1  1.2
M  1  1.3
F  2  1.4
M  2  1.5
F  2  1.6
M  2  1.7
;


/* result1 (unweighted) */
proc freq data=test;
tables sex * group / chisq ;
run;


/* result2 (weighted) */
proc freq data=test;
tables sex * group / chisq ;
weight weight;
run; 

下面的SAS-result1(未加权)可以在R中复制

enter image description here

test <-  structure(list(sex = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("F", "M"), class = "factor"), 
    group = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L), weight = c(0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 
    1.5, 1.6, 1.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 
    1.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7)), .Names = c("sex", 
"group", "weight"), class = "data.frame", row.names = c(NA, -30L
)) 


table( test$sex, test$group)
chisq.test( table( test$sex, test$group) )

在R:

enter image description here

SAS-result2,我不知道如何做R:

enter image description here

1 个答案:

答案 0 :(得分:2)

library(weights)
wtd.chi.sq(test$sex, test$group, weight = test$weight)
#      Chisq          df     p.value 
#0.002215526 1.000000000 0.962457933