我有一个模型,其中包含一个连续的预测变量和一个类别的预测变量。
下面的代码是检验均值和方差之间的差异的正确方法吗?
short<-function(t) {
str(t)
print("model")
m<-lm(y~x*r,data=t,na.action=na.omit,weights=t$weights)
print("summary(m)")
print(summary(m))
print("summary(aov)")
print(summary(aov(m)))
print("test for equal means")
print((t.test(y ~ r, data=t)))
print("test for equal variances")
print((var.test(y ~ r, data=t)))
}
它打印出来:
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 81 obs. of 10 variables:
$ a : num 7.5 6.8 6.74 6.97 7.3 7.08 7.05 7.2 7.8 8.08 ...
$ x : num 144 103 106 110 122 ...
$ y : num 0.42 0.68 0.64 0.55 0.49 0.431 0.421 0.53 0.164 0.084 ...
$ n : num 265 250 122 122 296 439 439 188 455 459 ...
$ tx : num 24 24 20 20 52 26 26 26 24 24 ...
$ dm2 : num 4.8 3.9 7.9 9 13 10.1 9.6 9.5 12.6 12.3 ...
$ race : num 1 1 1 1 1 1 1 1 1 1 ...
$ size : num 0.0124 0.0117 0.00571 0.00571 0.01385 ...
$ weights: num 0.0124 0.0117 0.00571 0.00571 0.01385 ...
$ r : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
[1] "model"
[1] "summary(m)"
Call:
lm(formula = y ~ x * r, data = t, weights = t$weights, na.action = na.omit)
Weighted Residuals:
Min 1Q Median 3Q Max
-0.028252 -0.007268 0.002292 0.008745 0.029854
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.246985 0.102363 12.182 < 2e-16 ***
x -0.006364 0.000808 -7.877 1.76e-11 ***
r2 -0.280859 0.241185 -1.164 0.248
x:r2 0.001786 0.002005 0.891 0.376
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.0123 on 77 degrees of freedom
Multiple R-squared: 0.4713, Adjusted R-squared: 0.4507
F-statistic: 22.88 on 3 and 77 DF, p-value: 1.084e-10
[1] "summary(aov)"
Df Sum Sq Mean Sq F value Pr(>F)
x 1 0.009681 0.009681 63.968 1.03e-11 ***
r 1 0.000588 0.000588 3.882 0.0524 .
x:r 1 0.000120 0.000120 0.793 0.3758
Residuals 77 0.011654 0.000151
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
[1] "test for equal means"
Welch Two Sample t-test
data: y by r
t = 1.1229, df = 35.039, p-value = 0.2691
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.03406304 0.11838766
sample estimates:
mean in group 1 mean in group 2
0.4479123 0.4057500
[1] "test for equal variances"
F test to compare two variances
data: y by r
F = 2.3944, num df = 64, denom df = 15, p-value = 0.06285
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.9514814 4.9001925
sample estimates:
ratio of variances
2.394376
>