你不指定wtd.t.test功能來自哪個包從,所以我會假設使用的函數從「權重」包。根據文件,前兩個參數是來自兩組的數據,第三和第四個參數是兩組觀測值的權重。如果未提供第四個參數,則給定的權重將用於兩個組。這意味着您編寫的代碼正在測試Ya1的加權平均值是否與sec1的加權平均值不同。這看起來不像你想要做的。我覺得LM是你的使用情況更適合:
# Make some example data
sec1 <- factor(sample(0:1, replace=TRUE, size=700))
Ya1 <- rnorm(700) + as.numeric(sec1)
weights1T <- 1.4^(rnorm(700))
# Use lm() to perform a weighted t-test
summary(lm(Ya1 ~ sec1, weights=weights1T))
這給:
> summary(lm(Ya1 ~ sec1, weights=weights1T))
Call:
lm(formula = Ya1 ~ sec1, weights = weights1T)
Weighted Residuals:
Min 1Q Median 3Q Max
-3.1921 -0.6672 -0.0374 0.7025 4.4411
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.92035 0.05376 17.12 <2e-16 ***
sec11 1.11120 0.07874 14.11 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1.061 on 698 degrees of freedom
Multiple R-squared: 0.222, Adjusted R-squared: 0.2209
F-statistic: 199.1 on 1 and 698 DF, p-value: < 2.2e-16
如果你真的想使用wtd.t.test
,你可以這樣做是這樣的:
library(weights)
ysplit <- split(Ya1, sec1)
wsplit <- split(weights1T, sec1)
wtd.t.test(y1split[[1]], y1split[[2]], w1split[[1]], w1split[[2]])
它給出了幾乎與lm()
相同的答案:
> wtd.t.test(x=ysplit[[1]], y=ysplit[[2]],
+ weight=wsplit[[1]], weighty=wsplit[[2]])
$test
[1] "Two Sample Weighted T-Test (Welch)"
$coefficients
t.value df p.value
-13.50571 697.25403 0.00000
$additional
Difference Mean.x Mean.y Std. Err
-1.00357229 1.04628894 2.04986124 0.07430724
Warning message:
In wtd.t.test(y1split[[1]], y1split[[2]], w1split[[1]], w1split[[2]]) :
Treating data for x and y separately because they are of different lengths
你應該指定你正在使用的軟件包,因爲'wtd.t.test'不在基本狀態 – C8H10N4O2