## [1] 9.181
## [1] 5.234965
##
## Shapiro-Wilk normality test
##
## data: x
## W = 0.92341, p-value = 0.3863
##
## One Sample t-test
##
## data: x
## t = 0.653, df = 9, p-value = 0.5301
## alternative hypothesis: true mean is not equal to 8.1
## 95 percent confidence interval:
## 5.436132 12.925868
## sample estimates:
## mean of x
## 9.181
##
## One Sample t-test
##
## data: x
## t = 0.653, df = 9, p-value = 0.265
## alternative hypothesis: true mean is greater than 8.1
## 99 percent confidence interval:
## 4.510276 Inf
## sample estimates:
## mean of x
## 9.181
## extra group ID
## 1 0.7 1 1
## 2 -1.6 1 2
## 3 -0.2 1 3
## 4 -1.2 1 4
## 5 -0.1 1 5
## 6 3.4 1 6
## 'data.frame': 20 obs. of 3 variables:
## $ extra: num 0.7 -1.6 -0.2 -1.2 -0.1 3.4 3.7 0.8 0 2 ...
## $ group: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ ID : Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
##
## Shapiro-Wilk normality test
##
## data: extra[group == 2] - extra[group == 1]
## W = 0.82987, p-value = 0.03334
##
## Wilcoxon signed rank test with continuity correction
##
## data: extra[group == 2] - extra[group == 1]
## V = 45, p-value = 0.009091
## alternative hypothesis: true location is not equal to 0
##
## Paired t-test
##
## data: extra[group == 1] and extra[group == 2]
## t = -4.0621, df = 9, p-value = 0.002833
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -2.4598858 -0.7001142
## sample estimates:
## mean of the differences
## -1.58
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00 1.05 1.30 1.58 1.70 4.60
stripchart(sleep1, method = 'stack', xlab = 'hours', main = 'Sleep prolongation (n = 10)')
boxplot(sleep1, horizontal = TRUE, add = TRUE, at = .6, pars = list(boxwex=0.5, staplewex=1)) # add = 그래프 결합여부, at = 그래프의 위치, boxwex, staplewex, outwex는 각각 상자의 크기, 수염의 길이, 이상치까지의 거리
## sex age
## 1 Female 115.1848
## 2 Male 126.0871
# 원래는 정규분포가정도 확인해야합니다 (n 수가 많아서 그냥 넘어감)
# 여성의 분포는 정규분포 하지 않는 것으로 확인됩니다.
shapiro.test(acs$age[acs$sex == "Female"])##
## Shapiro-Wilk normality test
##
## data: acs$age[acs$sex == "Female"]
## W = 0.96138, p-value = 6.34e-07
##
## Shapiro-Wilk normality test
##
## data: acs$age[acs$sex == "Male"]
## W = 0.99631, p-value = 0.2098
##
## F test to compare two variances
##
## data: age by sex
## F = 0.91353, num df = 286, denom df = 569, p-value = 0.3866
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.7495537 1.1209741
## sample estimates:
## ratio of variances
## 0.9135342
##
## Ansari-Bradley test
##
## data: age by sex
## AB = 57724, p-value = 0.02265
## alternative hypothesis: true ratio of scales is not equal to 1
##
## Two Sample t-test
##
## data: age by sex
## t = 10.071, df = 855, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 6.493487 9.637377
## sample estimates:
## mean in group Female mean in group Male
## 68.67596 60.61053
##
## Welch Two Sample t-test
##
## data: age by sex
## t = 10.222, df = 596.99, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 6.515847 9.615016
## sample estimates:
## mean in group Female mean in group Male
## 68.67596 60.61053
library(plyr)
ageSummary <- ddply(acs, "sex", summarize,
age.mean = mean(age), age.sd = sd(age),
Lower = age.mean - 2*age.sd /sqrt(NROW(age)),
Upper = age.mean + 2*age.sd /sqrt(NROW(age)))
ageSummary## sex age.mean age.sd Lower Upper
## 1 Female 68.67596 10.73242 67.40893 69.94299
## 2 Male 60.61053 11.22885 59.66988 61.55118
ggplot(ageSummary, aes(x = age.mean, y = sex)) +
geom_point() +
geom_errorbarh(aes(xmin = Lower, xmax = Upper), height = 0.2)
## Dx LDLC.1 LDLC.2
## 1 NSTEMI 126.09459 44.73373
## 2 STEMI 116.72449 39.45286
## 3 Unstable Angina 112.87724 40.38533
## Df Sum Sq Mean Sq F value Pr(>F)
## Dx 2 18765 9382 5.617 0.00377 **
## Residuals 830 1386305 1670
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 24 observations deleted due to missingness
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = LDLC ~ Dx, data = acs)
##
## $Dx
## diff lwr upr p adj
## STEMI-NSTEMI -9.370105 -19.04130 0.3010954 0.0599378
## Unstable Angina-NSTEMI -13.217357 -22.47817 -3.9565482 0.0024227
## Unstable Angina-STEMI -3.847252 -11.25450 3.5599980 0.4419434
* y축 라벨이 축에 평행하게 되어 잇습니다. 축의 라벨은 par() 함수의 las 인수로 조절 합니다. 하지만 par 함수의 인수조절은 향후 모든 그래프에 영향을 주므로 반드시 원래의 default 값으로 회복시키는 것이 좋습니다.
## [1] 451 457
##
## Shapiro-Wilk normality test
##
## data: resid(out)
## W = 0.97137, p-value = 1.024e-11
##
## Anderson-Darling normality test
##
## data: resid(out)
## A = 2.5798, p-value = 1.636e-06
shapiro.test()로 잔차의 정규성을 검정한 결과도 qqPlot과 마찬가지로 정규분포 가정을 만족시키지 않습니다.
등분산 가정은 Bartlett’s test로 검정할 수 있습니다. bartlett.test(반응변수~그룹변수, data=데이터)
##
## Bartlett test of homogeneity of variances
##
## data: LDLC by Dx
## Bartlett's K-squared = 3.3668, df = 2, p-value = 0.1857
Bartlett 검정결과도 p-value가 유의하지 않으므로 세 군의 분산은 유의하게 다르지 않다고 할 수 있습니다.
또한 등분산 검정은 이상치(outlier)에 민감하므로 이상치가 있는지 검정해볼 필요가 있는데 이를 위해서는 car 패키지에 있는 outlierTest()를 사용할 수 있습니다.
## rstudent unadjusted p-value Bonferroni p
## 457 6.013561 2.7194e-09 2.2653e-06
## 451 4.998183 7.0625e-07 5.8830e-04
## 789 4.058303 5.4106e-05 4.5070e-02
* 반응변수가 정규분포하는지 검사하기 위해서 먼저 lm() 함수를 사용하여 선형모형에 적합시킨 후 잔차를 shapiro.test()에 넣어주어야 합니다.
##
## Shapiro-Wilk normality test
##
## data: resid(out)
## W = 0.99343, p-value = 0.000808
##
## Wilcoxon rank sum test with continuity correction
##
## data: age by sex
## W = 115271, p-value < 2.2e-16
## alternative hypothesis: true location shift is not equal to 0
##
## Kruskal-Wallis rank sum test
##
## data: LDLC by factor(Dx)
## Kruskal-Wallis chi-squared = 10.733, df = 2, p-value = 0.004669
##
## #----------------Nonparametric Multiple Comparisons for relative effects---------------#
##
## - Alternative Hypothesis: True differences of relative effects are not equal to 0
## - Estimation Method: Global Pseudo Ranks
## - Type of Contrast : Tukey
## - Confidence Level: 95 %
## - Method = Fisher with 290 DF
##
## #--------------------------------------------------------------------------------------#
##
##
## #----------------Nonparametric Multiple Comparisons for relative effects---------------#
##
## - Alternative Hypothesis: True differences of relative effects are not equal to 0
## - Estimation Method: Global Pseudo ranks
## - Type of Contrast : Tukey
## - Confidence Level: 95 %
## - Method = Fisher with 290 DF
##
## #--------------------------------------------------------------------------------------#
##
## #----Data Info-------------------------------------------------------------------------#
## Sample Size Effect Lower Upper
## 1 NSTEMI 148 0.5466111 0.5189087 0.5740277
## 2 STEMI 294 0.4961078 0.4723244 0.5199088
## 3 Unstable Angina 391 0.4572811 0.4350779 0.4796554
##
## #----Contrast--------------------------------------------------------------------------#
## 1 2 3
## 2 - 1 -1 1 0
## 3 - 1 -1 0 1
## 3 - 2 0 -1 1
##
## #----Analysis--------------------------------------------------------------------------#
## Estimator Lower Upper Statistic p.Value
## 2 - 1 -0.051 -0.117 0.016 -1.785 0.174788691
## 3 - 1 -0.089 -0.152 -0.026 -3.310 0.002989234
## 3 - 2 -0.039 -0.092 0.014 -1.725 0.195779428
##
## #----Overall---------------------------------------------------------------------------#
## Quantile p.Value
## 1 2.350988 0.002989234
##
## #--------------------------------------------------------------------------------------#
##
## Pairwise comparisons using Wilcoxon rank sum test with continuity correction
##
## data: acs$LDLC and acs$Dx
##
## NSTEMI STEMI
## STEMI 0.2498 -
## Unstable Angina 0.0041 0.2462
##
## P value adjustment method: bonferroni
##
## Descriptive Statistics by 'Dx'
## ____________________________________________________________________
## NSTEMI STEMI Unstable Angina p
## (N=153) (N=304) (N=400)
## --------------------------------------------------------------------
## LDLC 123.0 [99.0;146.0] 115.0 [91.0;142.0] 111.0 [83.0;137.0] 0.005
## --------------------------------------------------------------------