독립표본 t-test 분석

특성

  • 두개의 독립적인 표본 간에 하나의 종속변수의 평균이 동일한가를 검증

예1) 남녀직원들 간에 회사에 대한 만족 수준은 같은가?,다른가?

예2) 결혼한 사람과 결혼하지 않은 사람 중 누가 더 행복한가?

독립표본 t-test 분석의 가설 및 검정통계량

  • 귀무가설: 남녀 직원 간에 평균 월급에는 차이가 없다.

  • 대립가설: 남녀 직원 간에 평균 월급에는 차이가 있다.

  • 검정통계량 : t-value, 자유도(df), 이를 고려하여 유의확률(p)의 계산

  • 검정통계량 = 두집단의 평균차이/ 표준오차(SE)

  • 표준오차는 표본 평균에 대한 표준 편차로 정의됩니다.

  • 독립변수가 범주형자료이고 종속변수가 연속형자료일때 t-test 또는 분산분석(ANOVA)을 한다

  • 독립변수 범주가 2개일때 t-test

  • 독립변수 범주가 3개 이상일때 분산분석

실습

독립변수 t-test 분석대상 변수는 무엇일까?

  • 성별 (남,여)

t1

1
a <- read.csv('cosmetics.csv', header=T)
1
a
gendermarriageedujobmincomeawarecountamountdecisionpropensityskinpromolocationsatisf_bsatisf_isatisf_alrepurchase
1 1 4 1 2 2 1 110002 1 1 1 2 5 2 2 2
2 1 4 9 2 1 4 300001 1 3 2 3 2 3 3 4
2 2 4 4 3 1 6 1000003 2 3 2 2 4 5 4 4
2 2 4 7 5 2 6 650003 2 5 2 3 3 4 4 4
1 2 6 6 5 2 2 500002 2 3 2 3 3 3 3 3
2 2 2 7 3 1 2 1000002 1 4 2 3 3 4 4 3
2 1 6 4 5 1 5 1000003 2 5 2 3 2 2 3 4
1 1 6 4 5 4 10 390003 2 2 1 2 4 4 4 4
2 2 4 5 2 2 2 400003 2 3 2 3 3 4 4 4
2 2 4 5 2 1 2 1000003 3 3 1 3 2 3 4 4
2 1 7 4 3 10 3 500001 3 1 2 3 3 3 4 4
1 1 2 5 3 2 1 300003 2 3 2 2 3 3 3 3
2 2 4 4 3 4 4 3200002 3 3 3 2 4 4 4 4
2 2 4 4 2 3 2 2000001 2 3 1 3 3 3 3 3
1 2 4 4 6 2 2 600003 2 1 2 5 3 3 3 4
2 1 4 5 2 2 3 500001 2 4 1 3 3 4 3 3
1 2 8 3 2 5 3 10000001 3 1 2 2 3 3 3 3
2 1 3 8 5 1 6 15000003 3 2 4 1 4 4 4 4
1 2 2 6 2 4 1 800002 3 1 2 3 3 3 4 4
1 1 4 4 3 8 3 300002 2 3 2 3 3 3 3 3
2 2 2 4 2 8 4 3500003 2 3 2 2 3 4 4 4
2 2 4 7 6 1 4 2500003 3 2 2 3 2 3 4 4
2 2 4 7 3 1 25 500002 2 1 2 3 3 4 4 4
2 2 2 9 1 1 1 200001 1 5 1 3 3 3 3 3
1 1 3 8 4 2 3 420001 2 3 1 3 3 3 4 4
1 1 4 8 4 2 3 420003 3 2 1 3 3 4 4 4
2 1 4 4 3 2 20 400003 1 5 2 3 2 4 4 4
2 2 4 4 6 1 6 700003 3 5 2 1 3 4 4 4
2 2 8 4 5 5 6 2000002 1 4 1 1 4 4 4 3
1 2 4 2 6 2 1 2000003 2 1 2 2 3 4 4 4
...................................................
2 2 4 5 2 1 10 300002 2 2 4 3 3 4 4 4
1 2 4 6 6 7 5 500003 2 4 2 2 3 4 4 4
1 2 4 1 4 1 1 100001 3 1 1 1 5 1 3 1
1 1 4 4 3 2 1 100001 1 4 3 2 2 3 3 3
1 1 4 4 3 2 3 500003 2 5 2 3 2 4 4 4
1 2 2 5 4 2 1 600001 1 1 4 5 3 3 3 3
1 1 6 1 3 2 2 500003 2 5 1 2 4 4 3 4
2 2 6 4 6 1 3 5000003 3 5 2 1 3 4 4 4
1 2 4 1 3 2 1 500003 2 1 2 3 2 3 3 3
2 2 6 3 4 1 1 1000003 2 1 2 1 3 3 3 3
2 2 4 7 4 1 2 500003 1 5 2 2 3 2 3 4
2 2 4 4 2 2 12 200002 2 3 2 3 3 3 3 3
1 1 4 4 4 2 4 350002 2 1 3 2 3 3 4 4
1 1 4 4 4 2 4 300002 2 1 2 3 3 4 4 3
2 2 4 7 1 2 2 500001 1 3 2 5 3 4 4 4
2 2 4 7 1 2 3 500001 2 3 2 5 3 3 3 3
2 1 4 4 2 2 7 800003 2 3 2 2 3 3 3 3
1 1 4 1 3 2 6 200003 1 3 1 3 2 3 4 4
1 1 4 10 2 2 2 250003 1 3 2 3 3 4 4 4
2 1 3 8 1 1 7 1000002 1 5 2 3 3 3 1 2
1 2 4 4 3 2 2 500001 2 5 2 2 3 4 3 3
1 2 4 4 5 2 1 800002 2 1 2 3 3 3 3 3
2 2 6 7 5 1 2 3000003 3 2 3 2 3 4 3 3
2 2 7 7 4 1 2 2000002 2 4 3 2 3 3 3 3
1 1 2 1 2 2 5 30001 1 2 1 1 1 1 1 1
1 1 4 2 3 2 6 40001 1 1 1 4 2 1 1 1
2 2 4 4 2 1 10 1500003 2 2 1 2 3 4 4 4
2 2 7 8 1 2 3 1000001 2 1 1 5 2 5 4 4
1 1 4 6 1 3 2 200003 1 1 1 3 4 3 3 2
2 2 6 10 1 1 10 10000003 2 3 1 3 2 3 3 3

기술통계 방법 - aggregate() 함수 사용 , tapply() 함수 사용

1
aggregate(count~gender, a, mean) # count는 연구매횟수
gendercount
1 3.227273
2 5.591304
1
2
attach(a)
tapply(count,gender,mean)
1
2
	1    3.22727272727273
	2    5.59130434782609
1
tapply(count,gender,sd) # 표준편차 보고싶을때 sd사용하기
1
2
3
4
5
6
7
8
	1
	3.24235073777212

	2
	5.20609570544753


Rcmdr: R Commander

  • A platform-independent basic-statistics GUI (graphical user interface) for R, based on the tcltk package.
1
2
install.packages('Rcmdr')
library(Rcmdr)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
also installing the dependencies 'png', 'jpeg', 'checkmate', 'viridisLite', 'survival', 'Formula', 'latticeExtra', 'htmlTable', 'viridis', 'proxy', 'mitools', 'sandwich', 'Hmisc', 'e1071', 'readstata13', 'nortest', 'survey', 'estimability', 'insight', 'RcmdrMisc', 'effects', 'tcltk2', 'relimp'




  There are binary versions available but the source versions are later:
             binary source needs_compilation
jpeg        0.1-8.1  0.1-9              TRUE
survival     3.2-11 3.2-13              TRUE
htmlTable     2.1.0  2.4.0             FALSE
viridis       0.6.1  0.6.2             FALSE
proxy        0.4-25 0.4-26              TRUE
sandwich      3.0-0  3.0-1             FALSE
Hmisc         4.5-0  4.6-0              TRUE
e1071         1.7-6  1.7-9              TRUE
readstata13   0.9.2 0.10.0              TRUE
survey          4.0  4.1-1             FALSE
insight      0.14.0 0.15.0             FALSE
RcmdrMisc     2.7-1  2.7-2             FALSE
effects       4.2-0  4.2-1             FALSE
Rcmdr         2.7-1  2.7-2             FALSE

  Binaries will be installed
package 'png' successfully unpacked and MD5 sums checked
package 'jpeg' successfully unpacked and MD5 sums checked
package 'checkmate' successfully unpacked and MD5 sums checked
package 'viridisLite' successfully unpacked and MD5 sums checked
package 'survival' successfully unpacked and MD5 sums checked
package 'Formula' successfully unpacked and MD5 sums checked
package 'latticeExtra' successfully unpacked and MD5 sums checked
package 'proxy' successfully unpacked and MD5 sums checked
package 'mitools' successfully unpacked and MD5 sums checked
package 'Hmisc' successfully unpacked and MD5 sums checked
package 'e1071' successfully unpacked and MD5 sums checked
package 'readstata13' successfully unpacked and MD5 sums checked
package 'nortest' successfully unpacked and MD5 sums checked
package 'estimability' successfully unpacked and MD5 sums checked
package 'tcltk2' successfully unpacked and MD5 sums checked
package 'relimp' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\MyCom\AppData\Local\Temp\RtmpSQXZ2X\downloaded_packages


installing the source packages 'htmlTable', 'viridis', 'sandwich', 'survey', 'insight', 'RcmdrMisc', 'effects', 'Rcmdr'

Warning message in install.packages("Rcmdr"):
"installation of package 'effects' had non-zero exit status"Warning message in install.packages("Rcmdr"):
"installation of package 'Rcmdr' had non-zero exit status"


Error in library(Rcmdr): there is no package called 'Rcmdr'
Traceback:


1. library(Rcmdr)

psych: Procedures for Psychological, Psychometric, and Personality Research

1
library(psych)
1
describeBy(count,group=gender) # 훨씬 많은 것을 볼수 있다.
1
2
3
4
5
6
7
8
 Descriptive statistics by group
group: 1
   vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
X1    1 132 3.23 3.24      2    2.52 1.48   1  20    19 2.91    10.35 0.28
------------------------------------------------------------
group: 2
   vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
X1    1 115 5.59 5.21      4    4.65 2.97   1  36    35 2.81    10.72 0.49

등분산성 테스트

  • t-test분석 및 분산분석할때 기본적으로 만족해야되는 조건 중 한가지로, 분석하는 집단들의 분산이 같음을 의미한다.

  • 검정의 방법은 두 집단, 다 집단일때 달라진다. 또한 모수, 비모수에 따라 달라진다.

두 집단일때 ftest 사용함

귀무가설 : 등분산성이 있다.(분산의 차이가 없다.)

대립가설 : 등분산성이 없다.(분산의 차이가 있다.)

다 집단일때 bartlett.test() 사용함

귀무가설 : 등분산성이 있다.(분산의 차이가 없다.)

대립가설 : 등분산성이 없다.(분산의 차이가 있다.)

  • LenveneTest는 car package에 포함되어 있으니 일단은 library(car)를 통하여 이 패키지를 불러와야 한다.

등분산, 이분산 차이

t2

1
2
install.packages('car')
library(car)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
also installing the dependency 'lme4'




  There are binary versions available but the source versions are later:
     binary   source needs_compilation
lme4 1.1-26 1.1-27.1              TRUE
car  3.0-10   3.0-12             FALSE

  Binaries will be installed
package 'lme4' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\MyCom\AppData\Local\Temp\RtmpSQXZ2X\downloaded_packages


installing the source package 'car'
1
2
3
4
# 성별에 따른 연구매횟수 확인하기

leveneTest(count,factor(gender),center=mean) #두집단의 분산이 '서로 같나?'를 확인할때 사용한다,
# Pr값이 0.05보다 작으므로 등분산이 아니라 이분산이다.
DfF valuePr(>F)
group 1 10.81286 0.001155889
245 NA NA
1
2
# 이분산 검증
t.test(count~gender, var.equal=F) # 3.977e-05  : 소수 다섯점까지 나타냄
1
2
3
4
5
6
7
8
9
10
	Welch Two Sample t-test

data:  count by gender
t = -4.2099, df = 185.62, p-value = 3.977e-05
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -3.471848 -1.256215
sample estimates:
mean in group 1 mean in group 2
       3.227273        5.591304
  • 두 집단의 분산이 서로 같다면 t값(검정통계량)이 0이 되고 p-value값이 1이 된다. 하지만 두 집단의 차이는 크다!
1
2
3
# 성별에 따른 전반만족도 확인하기

describeBy(satisf_al,group=gender)
1
2
3
4
5
6
7
8
 Descriptive statistics by group
group: 1
   vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
X1    1 132 3.44 0.71      3    3.49 1.48   1   5     4 -0.98     2.18 0.06
------------------------------------------------------------
group: 2
   vars   n mean  sd median trimmed  mad min max range  skew kurtosis   se
X1    1 115 3.49 0.8      4    3.54 1.48   1   5     4 -0.98        2 0.07
1
2
leveneTest(satisf_al,factor(gender),center=mean) #종속변수, 독립변수
# p값이 0.05보다 크므로 등분산이다.
DfF valuePr(>F)
group 1 0.897477 0.3443926
245 NA NA

분산이 동일한 경우 var.equal을 TRUE로 설정하고, 동일하지 않은 경우 FALSE로 설정해야 합니다.

  • P값이 0.05보다 작으면 var.equal=F로 설정 그 반대면 T로 설정해준다.

  • P값이 0.05 이상이면 분산이 동일하다고 할 수 있고 , 0.05 이하이면 분산이 동일하다고 할 수 없다.

1
2
t.test(satisf_al~gender,var.equal=T)  #P값이 0.05보다 크므로 var.equal를 T로 설정해준다.

1
2
3
4
5
6
7
8
9
10
	Welch Two Sample t-test

data:  satisf_al by gender
t = -0.49072, df = 230.47, p-value = 0.6241
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.2385333  0.1434081
sample estimates:
mean in group 1 mean in group 2
       3.439394        3.486957

결론 - 남녀간의 전반만족도 차이가 없다.( 귀무가설이 참일 가능성이 높다.)

그래프 그리기

1
library(ggplot2)
1
2
3
4
5
6
7
8
9
10
Registered S3 methods overwritten by 'tibble':
  method     from
  format.tbl pillar
  print.tbl  pillar

Attaching package: 'ggplot2'

The following objects are masked from 'package:psych':

    %+%, alpha
1
2
a$gender<-factor(a$gender, levels=c(1,2), labels=c('남','여'))
ggplot(a,aes(x=as.factor(gender),y=count)) + geom_boxplot(fill='slateblue', alpha= 0.2) + xlab('gender')

output_32_0

1
ggplot(a,aes(x=as.factor(gender),y=satisf_al)) + geom_boxplot(fill='blue', alpha= 0.2) + xlab('gender')

output_33_0

1
ggplot(data=a, aes(x=count, group=gender, fill=gender)) + geom_density(adjust=1.5)

output_34_0

1

Meta Info

Categories:

Published At:

Modified At:

Leave a comment