남녀 등 두 집단의 평균 차이를 분석할 때는 독립표본 t검정을 한다
R에서는 내장된 t.test()함수로 한다
독립변수는 명목척도이며, 종속변수는 등간척도 또는 비율척도이어야 한다.
t.test()함수 사용 방식은 2가지 방법1. t.test(data=데이터세트,종속변수(비교값)~독립변수(비교대상)) 방법2. t.test(데이터세트\(종속변수(비교값)~데이터세트\)독립변수(비교대상))
예제파일인 mpg1.csv의 trans변수에는 기어변속방법으로 auto(자동식)와 manual(수동식)등 두 방식이 있다. 두 방식에 cty 평균에 통계적으로 유의미한 차이가 있는지 알아보자. cty는 도시에서 1갤런당 달리는 거리. 독립변수는 trans이며, 종속변수는 cty이다. 가설은 다음과 같이 설정한다.
mpg1 <- read.csv("mpg1.csv")
t.test(data=mpg1, cty~trans) #t.test(mpg1$cty~mpg1$tran)도 같음
##
## Welch Two Sample t-test
##
## data: cty by trans
## t = -4.5375, df = 132.32, p-value = 1.263e-05
## alternative hypothesis: true difference in means between group auto and group manual is not equal to 0
## 95 percent confidence interval:
## -3.887311 -1.527033
## sample estimates:
## mean in group auto mean in group manual
## 15.96815 18.67532
교차분석은 범주형 변수들이 관계가 있다는 것을 입증하는 것
평균의 차이가 아니라, 비율에 차이가 있는지를 검정
교차분석 검정은 R의 chisq.test() 함수 사용
mpg1.csv를 mpg1로 불러온다. mpg1에 있는 trans(기어 변속방식)변수의 범주에 따라 drv(구동방식) 범주의 비율에 차이가 있는가를 알아본다. 연구가설은 다음과 같이 설정.
우선 table() 함수와 prop.table()함수로 교차분석을 해서 trans에 따른 drv의 빈도와 비율을 알아본다.
mpg1 <- read.csv("mpg1.csv", stringsAsFactors = F)
table(mpg1$trans, mpg1$drv) # trans와 drv의 교차분석
##
## 4 f r
## auto 75 65 17
## manual 28 41 8
prop.table(table(mpg1$trans, mpg1$drv),1) # auto와 manual의 drv 비율 분석
##
## 4 f r
## auto 0.4777070 0.4140127 0.1082803
## manual 0.3636364 0.5324675 0.1038961
auto에서는 4륜구동(4)인 47.8%로 가장 많고, manual에서는 전륜구동(f)이 53.2%이 가장 많아서 trans에 따라 drv에 차이가 있는 것 같다. 이에 대해 정말 그런지, 통계적으로 분석하는 것이 교차분석이다.
방법 1
chisq.test(mpg1$trans, mpg1$drv)
##
## Pearson's Chi-squared test
##
## data: mpg1$trans and mpg1$drv
## X-squared = 3.1368, df = 2, p-value = 0.2084
chisq.test(table(mpg1$trans, mpg1$drv))
##
## Pearson's Chi-squared test
##
## data: table(mpg1$trans, mpg1$drv)
## X-squared = 3.1368, df = 2, p-value = 0.2084
summary(table(mpg1$trans, mpg1$drv))
## Number of cases in table: 234
## Number of factors: 2
## Test for independence of all factors:
## Chisq = 3.1368, df = 2, p-value = 0.2084
#cor.test(데이터세트$비교변수1, 데이터세트$비교변수2)
mpgl에는 cty와 hwy가 있다
cty는 도시에서 1갤런당 달리는 거리
hwy는 고속도로에서 1갤런당 달리는 거리
cty가 길면 hwy도 길 것이라 생각할 수 있다. 이 가설을 검정해본다. 검정하려는 가설은 cty와 hwy는 서로 상관관계가 있다는 것이기 때문에 이것이 대립가설. 귀무가설은 상관관계가 없다는 것이다.
mpg1 <- read.csv("mpg1.csv", stringsAsFactors = F)
cor.test(mpg1$cty, mpg1$hwy) #상관관계분석
##
## Pearson's product-moment correlation
##
## data: mpg1$cty and mpg1$hwy
## t = 49.585, df = 232, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.9433129 0.9657663
## sample estimates:
## cor
## 0.9559159
결과를 보면 ’p-value < 2.2e-15’이다. 유의수준이 2.2/10^16보다 작다. 유의수준(p<.05)안에 있다. 이 검정의 결과로 귀무가설을 기각하고 대립가설을 채택할 수 있다. 대립가설은 “상관관계는 0이 아니다(alternative hypothesis: true correlation is not equal to 0)”
결과의 sample estimates: 부분을 보면 상관관계는 0.9959159. 1에 가까우므로 매우 높다. 결과는 “cty와 hwy는 유의미하게 매우 높은 상관관계(r=.96)에 있다(p>.05)”
#다음 3가지 중 어느 것을 써도 된다.
#방법1 lm(data=데이터세트, 종속변수~독립변수)
#방법2 lm(종속변수~독립변수, data=데이터세트)
#방법3 lm(데이터세트$종속변수~데이터세트$독립변수)
R에 있는 mtcars 데이터로 분석한다. R에서 help(mtcars)를 하면 mtcars에 관한 정보를 알 수 있다. 어느 한 미국 잡지에 실렸던 데이터이며, 11개 변수에서 32개 자동차의 정보를 담고 있다. 11개의 변수 가운데 disp(배기량)가 mpg(1갤런당 주행 마일)에 미치는 여향을 분석해본다. str(mtcars)로 mtcars에 있는 변수들을 보면, disp와 mpg는 모두 실수형(num)변수이어서 회귀분석이 가능하다.
lm(data=mtcars, mpg~disp)
##
## Call:
## lm(formula = mpg ~ disp, data = mtcars)
##
## Coefficients:
## (Intercept) disp
## 29.59985 -0.04122
#lm(mpg~disp, data=mtcars), lm(mtcars$mpg~mtcars$disp)의 결과도 같다.
RA <- lm(data=mtcars, mpg~disp) #회귀분석 결과를 RA에 넣기
summary(RA) # 상세한 분석 결과 출력
##
## Call:
## lm(formula = mpg ~ disp, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.8922 -2.2022 -0.9631 1.6272 7.2305
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 29.599855 1.229720 24.070 < 2e-16 ***
## disp -0.041215 0.004712 -8.747 9.38e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.251 on 30 degrees of freedom
## Multiple R-squared: 0.7183, Adjusted R-squared: 0.709
## F-statistic: 76.51 on 1 and 30 DF, p-value: 9.38e-10
#방법1 lm(data=데이터세트, 종속변수~독립변수1+독립변수2+...)
#방법2 lm(종속변수~독립변수1+독립변수2+..., data=데이터세트)
#방법3 lm(데이터세트$종속변수~데이터세트$독립변수1+데이터세트$독립변수2+...)
lm(data=mtcars, mpg~disp+hp+wt)
##
## Call:
## lm(formula = mpg ~ disp + hp + wt, data = mtcars)
##
## Coefficients:
## (Intercept) disp hp wt
## 37.105505 -0.000937 -0.031157 -3.800891
#lm(mpg~disp+hp+wt, data=mtcars),
#lm(mtcars$mpg~mtcars$disp+mtcars$hp+mtcars$wt)의 결과도 같다.
RA <- lm(data=mtcars, mpg~disp+hp+wt) #회귀분석 결과를 RA 녛기
summary(RA)
##
## Call:
## lm(formula = mpg ~ disp + hp + wt, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.891 -1.640 -0.172 1.061 5.861
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.105505 2.110815 17.579 < 2e-16 ***
## disp -0.000937 0.010350 -0.091 0.92851
## hp -0.031157 0.011436 -2.724 0.01097 *
## wt -3.800891 1.066191 -3.565 0.00133 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.639 on 28 degrees of freedom
## Multiple R-squared: 0.8268, Adjusted R-squared: 0.8083
## F-statistic: 44.57 on 3 and 28 DF, p-value: 8.65e-11