在 R 中复制 SAS 加权频率和 chisq-test
Replicate SAS weighted frequencies and chisq-test in R
我正在尝试在 R 中复制 SAS 频率计数和 chisq 测试。
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
中复制
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 中:
SAS-result2,我不知道在 R 中怎么做:
library(weights)
wtd.chi.sq(test$sex, test$group, weight = test$weight)
# Chisq df p.value
#0.002215526 1.000000000 0.962457933
我正在尝试在 R 中复制 SAS 频率计数和 chisq 测试。
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
中复制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 中:
SAS-result2,我不知道在 R 中怎么做:
library(weights)
wtd.chi.sq(test$sex, test$group, weight = test$weight)
# Chisq df p.value
#0.002215526 1.000000000 0.962457933