检验、多样本均数比较的方差分析、卡方检验、秩转换的非参数检验,都是着重于描述某一变量的统计特征(或者叫分布情况)或比较该变量在不同组别之间的差别。但是在大量医学实践中,经常会遇到两个变量关系的研究,比如:两个变量有没有相关关系?是正相关/负相关?有没有直线相关或者曲线相关关系?
孙振球《医学统计学》第4版例9-1、例9-2、例9-3、例9-4。
某地方病研究所调查了8名正常儿童的尿肌酐含量(mmol/24h),估计尿肌酐含量(Y)对其年龄(X)的直线回归方程。
data9_1 <- data.frame(x = c(13,11,9,6,8,10,12,7),
y = c(3.54,3.01,3.09,2.48,2.56,3.36,3.18,2.65))建立回归方程,书中对于最小二乘法写了非常多内容,但是用代码就是1行即可:
fit <- lm(y ~ x, data = data9_1)
summary(fit) # 查看模型结果
##
## Call:
## lm(formula = y ~ x, data = data9_1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.21500 -0.15937 -0.00125 0.09583 0.30667
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.66167 0.29700 5.595 0.00139 **
## x 0.13917 0.03039 4.579 0.00377 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.197 on 6 degrees of freedom
## Multiple R-squared: 0.7775, Adjusted R-squared: 0.7404
## F-statistic: 20.97 on 1 and 6 DF, p-value: 0.003774截距是1.66167,x的系数是0.13917(也就是这条直线的斜率),对x这个变量进行假设检验得出:t=4.579,p=0.00377。
同时该结果也给出了回归方程的假设检验结果:F-statistic: 20.97,p-value: 0.003774。
例9-3,计算回归系数的95%的可信区间:
# β值的95%可信区间
confint(fit)
## 2.5 % 97.5 %
## (Intercept) 0.93493789 2.388395
## x 0.06480131 0.213532我们可以通过函数的方式分别获取模型信息:
# β值
coefficients(fit)
## (Intercept) x
## 1.6616667 0.1391667
# β值
coef(fit)
## (Intercept) x
## 1.6616667 0.1391667
# OR值
exp(coef(fit))
## (Intercept) x
## 5.268084 1.149316
# OR值的95%的可信区间
exp(confint(fit))
## 2.5 % 97.5 %
## (Intercept) 2.547055 10.895997
## x 1.066947 1.238043或者直接使用broom计算即可:
broom::tidy(fit,conf.int = T)
## # A tibble: 2 × 7
## term estimate std.error statistic p.value conf.low conf.high
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 1.66 0.297 5.59 0.00139 0.935 2.39
## 2 x 0.139 0.0304 4.58 0.00377 0.0648 0.214根据结果知:x的可信区间为:(0.06480131,0.213532)。
例9-4,当x=12时,计算总体均数的可信区间和个体Y值的预测区间,1行代码即可实现:
new_x <- data.frame(x=12)
# 总体均数的可信区间
predict(fit, newdata = new_x,interval = "confidence",level = 0.95)
## fit lwr upr
## 1 3.331667 3.079481 3.583852
# 个体Y值的预测区间
predict(fit, newdata = new_x,interval = "prediction",level = 0.95)
## fit lwr upr
## 1 3.331667 2.787731 3.875602以上结果均和书中一致。
孙振球《医学统计学》第4版例9-5、例9-6。
某医师测量了15名正常成年人的体重(kg)与CT双肾总体积(ml)大小,问:两变量是否有关联?其方向与密切程度如何?
data9_5 <- data.frame(
weight = c(43,74,51,58,50,65,54,57,67,69,80,48,38,85,54),
kv = c(217.22,316.18,231.11,220.96,254.70,293.84,263.28,271.73,263.46,
276.53,341.15,261.00,213.20,315.12,252.08)
)
str(data9_5)
## 'data.frame': 15 obs. of 2 variables:
## $ weight: num 43 74 51 58 50 65 54 57 67 69 ...
## $ kv : num 217 316 231 221 255 ...两变量是否有关联?其方向和密切程度如何?
直接用cor可计算相关系数r(又叫Pearson积差相关系数):
cor(data9_5$weight, data9_5$kv)
## [1] 0.8754315或者直接用cor.test,既可以计算相关系数,又可以计算相关系数的P值:
cor.test(data9_5$weight, data9_5$kv)
##
## Pearson's product-moment correlation
##
## data: data9_5$weight and data9_5$kv
## t = 6.5304, df = 13, p-value = 1.911e-05
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.6584522 0.9580540
## sample estimates:
## cor
## 0.8754315从结果可以看出,两者是正相关,相关系数r=0.8754,且P值小于0.05,并给出了相关系数的可信区间:(0.6584522~0.9580540),具有统计学意义!
可视化结果:
library(ggplot2)
ggplot(data9_5, aes(weight, kv)) +
geom_point(size = 4) +
geom_smooth(method = "lm",se=F) +
geom_vline(xintercept = mean(data9_5$weight),linetype=2)+
geom_hline(yintercept = mean(data9_5$kv),linetype=2)+
labs(x="体重(kg)X",y="双肾体积(ml)Y")+
theme_classic()
## `geom_smooth()` using formula = 'y ~ x'
相关性分析的过程比较简单,在选择方法时要注意是使用pearson相关还是秩相关。
决定系数R2的计算:
summary(lm(weight ~ kv, data = data9_5))
##
## Call:
## lm(formula = weight ~ kv, data = data9_5)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.947 -4.469 -1.338 4.285 12.500
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -23.1845 12.7869 -1.813 0.093 .
## kv 0.3109 0.0476 6.530 1.91e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.777 on 13 degrees of freedom
## Multiple R-squared: 0.7664, Adjusted R-squared: 0.7484
## F-statistic: 42.65 on 1 and 13 DF, p-value: 1.911e-05Multiple R-squared:0.7664, Adjusted R-squared(调整后的决定系数):0.7484
R2取值在0到1之间且无单位,其数值大小反映了回归贡献的相对程度,也就是在Y的总变异中回归关系所能解释的百分比。回归平方和(R2)越接近总平方和,则r(相关系数)绝对值越接近1,说明相关的实际效果越好。
本例r=0.875,得到R2=0.7656,表示此例中休重可解释双肾体积变异性的76.56%,另外约23%的变异不能用体重来解释。