본문 바로가기
  • plotly로 바로쓰는 동적시각화 in R & 파이썬
데이터 시각화

APA(미국심리학회) style 논문 회귀분석 결과표 만들기 in R - p 값의 표현

by 아참형인간 2022. 10. 31.
apa.knit

사용데이터 : https://2stndard.tistory.com/68

apaTables

R을 사용하여 논문을 작성하는 대학원생이나 연구자들은 열심히 분석한 결과를 논문에 사용해야할 때 겪는 문제가 있다. 바로 분석된 결과를 표로 만들어 논문에 싣는 과정이 쉽지 않다는 것이다. 보통 논문에서 사용하는 표의 형태로 많이 사용되는 형태가 APA style(American Psychological Association : 미국 심리학회) 이다. APA style의 표를 자동적으로 만들어 준다면 얼마나 편리할까? 이것을 가장 구현해 준 패키지가 apaTables 패키지이다. apaTables 패키지는 결과표를 MS-Word로 저장해주기 때문에 논문에서 바로 사용이 가능하다.

상관관계표(Correlation Table) : apa.cor.table()

상관관계표는 변수들간의 상관관계를 기술하는 표이다. 표의 가로축과 세로축에 같은 변수 배열이 나열되고 각각의 셀에 가로축의 변수와 세로축의 변수간의 상관관계계수를 표현해 준다. 보통 R에서 corrplot패키지를 사용하여 시각화하는 경우가 많은데 apaTables에서는 상관관계표를 APA style의 표로 자동적으로 만들어준다.

사용 데이터의 ’df_취업률_2000’에서 다음과 같이 상관관계행렬을 시각화해본다.

data(mtcars) #Load and preprocess data
mtcars$am <- as.factor(mtcars$am)
mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$vs <- as.factor(mtcars$vs)

mtcars |>
  select(mpg, disp, hp, wt, drat) |>
  cor()
##             mpg       disp         hp         wt       drat
## mpg   1.0000000 -0.8475514 -0.7761684 -0.8676594  0.6811719
## disp -0.8475514  1.0000000  0.7909486  0.8879799 -0.7102139
## hp   -0.7761684  0.7909486  1.0000000  0.6587479 -0.4487591
## wt   -0.8676594  0.8879799  0.6587479  1.0000000 -0.7124406
## drat  0.6811719 -0.7102139 -0.4487591 -0.7124406  1.0000000
library(corrplot)

mtcars |>
  select(mpg, disp, hp, wt, drat) |>
  cor() |> 
  corrplot()

이 시각화를 APA style의 표로 만든다면 apa.cor.table()을 사용하여 다음과 같이 만들 수 있다.

library(apaTables)

mtcars |>
  select(mpg, disp, hp, wt, drat) |>
  apa.cor.table(filename="Table1_APA.doc", table.number=1)  
## 
## 
## Table 1 
## 
## Means, standard deviations, and correlations with confidence intervals
##  
## 
##   Variable M      SD     1            2            3            4           
##   1. mpg   20.09  6.03                                                      
##                                                                             
##   2. disp  230.72 123.94 -.85**                                             
##                          [-.92, -.71]                                       
##                                                                             
##   3. hp    146.69 68.56  -.78**       .79**                                 
##                          [-.89, -.59] [.61, .89]                            
##                                                                             
##   4. wt    3.22   0.98   -.87**       .89**        .66**                    
##                          [-.93, -.74] [.78, .94]   [.40, .82]               
##                                                                             
##   5. drat  3.60   0.53   .68**        -.71**       -.45**       -.71**      
##                          [.44, .83]   [-.85, -.48] [-.69, -.12] [-.85, -.48]
##                                                                             
## 
## Note. M and SD are used to represent mean and standard deviation, respectively.
## Values in square brackets indicate the 95% confidence interval.
## The confidence interval is a plausible range of population correlations 
## that could have caused the sample correlation (Cumming, 2014).
##  * indicates p < .05. ** indicates p < .01.
## 

위의 결과를 워드에서 열어보면 다음과 같다.

위의 결과에서 보면 가로축의 변수와 세로축의 변수가 만나는 셀에 상관관계계수가 표현되는 것을 볼 수 있다. 하지만 이것은 cor() 만으로도 산출이 가능하다. 하지만 apa.cor.table()이 놀라운 것은 평균, 표준편차에 p 값의 범위, 95% 신뢰구간이 표현된다는 것이다. 아마도 논문을 써본 사람들이라면 이것이 얼마나 번거롭고 노가다 작업인지 알 것이다.

회귀분석 결과표 : apa.reg.table()

앞에서 본 상관관계표에서 표현되는 p 값의 범위, 95% 신뢰 구간이 자동으로 표현되는 것만해도 박수칠 만하다. 하지만 apaTables에서 제공하는 가장 핵심 기능은 바로 회귀분석 결과표이다. 회귀분석 결과표는 apa.reg.table()을 사용하여 만든다. 회귀 분석 결과표에는 표준화되지 않은 회귀 계수(신뢰구간이 표현된 \(b\)), 표준화된 회귀 계수(신뢰구간이 표현된 \(beta\)) ,부분 편상관계수(신뢰구간이 표현되 \(sr^2\)), 상관계수(\(r\)), 결정계수(\(R^2\)) 등이 표현된다.

apa.reg.table()을 사용하기 위해서는 먼저 회귀모델을 만들어야 하고 만든 회귀모델을 apa.reg.table()의 매개변수로 전달하면 된다.

lm_model <- lm(mpg ~ disp + hp, data = mtcars)

apa.reg.table(lm_model, filename = "Table2_APA.doc", table.number = 2)
## 
## 
## Table 2 
## 
## Regression results using mpg as the criterion
##  
## 
##    Predictor       b       b_95%_CI  beta    beta_95%_CI sr2  sr2_95%_CI      r
##  (Intercept) 30.74** [28.01, 33.46]                                            
##         disp -0.03** [-0.05, -0.02] -0.62 [-0.94, -0.31] .15  [.00, .29] -.85**
##           hp   -0.02  [-0.05, 0.00] -0.28  [-0.59, 0.03] .03 [-.03, .09] -.78**
##                                                                                
##                                                                                
##                                                                                
##              Fit
##                 
##                 
##                 
##      R2 = .748**
##  95% CI[.54,.83]
##                 
## 
## Note. A significant b-weight indicates the beta-weight and semi-partial correlation are also significant.
## b represents unstandardized regression weights. beta indicates the standardized regression weights. 
## sr2 represents the semi-partial correlation squared. r represents the zero-order correlation.
## Square brackets are used to enclose the lower and upper limits of a confidence interval.
## * indicates p < .05. ** indicates p < .01.
## 

위의 결과를 워드에서 열어보면 다음과 같다.

블록 회귀분석 결과표 : apa.reg.table()

어떤 경우에는 두 회귀 모델의 결과를 공통 변수와 비교할 떄가 있는데 이런 방식을 블록 기반 회귀라고 한다. 예를 들어 두 개의 독립 변수간의 상호작용이 있는 모델과 없는 모델을 하나의 표로 만들면 두 메델간의 비교가 쉬워진다. 이와 같은 블록 회귀분석도 apa.reg.table()로 만들수 있는데 여러 개의 모델을 매개변수로 전달해주면 만들어진다.

## 
## 
## Table 3 
## 
## Regression results using mpg as the criterion
##  
## 
##    Predictor       b       b_95%_CI sr2  sr2_95%_CI             Fit
##  (Intercept) 30.74** [28.01, 33.46]                                
##         disp -0.03** [-0.05, -0.02] .15  [.00, .29]                
##           hp   -0.02  [-0.05, 0.00] .03 [-.03, .09]                
##                                                         R2 = .748**
##                                                     95% CI[.54,.83]
##                                                                    
##  (Intercept) 39.67** [33.70, 45.64]                                
##         disp -0.07** [-0.10, -0.04] .17  [.02, .31]                
##           hp -0.10** [-0.15, -0.05] .10 [-.01, .21]                
##      disp:hp  0.00**   [0.00, 0.00] .07 [-.02, .16]                
##                                                         R2 = .820**
##                                                     95% CI[.64,.87]
##                                                                    
##         Difference
##                   
##                   
##                   
##                   
##                   
##                   
##                   
##                   
##                   
##                   
##  Delta R2 = .072**
##  95% CI[-.02, .16]
##                   
## 
## Note. A significant b-weight indicates the beta-weight and semi-partial correlation are also significant.
## b represents unstandardized regression weights. beta indicates the standardized regression weights. 
## sr2 represents the semi-partial correlation squared. r represents the zero-order correlation.
## Square brackets are used to enclose the lower and upper limits of a confidence interval.
## * indicates p < .05. ** indicates p < .01.
## 

위의 결과를 워드에서 열어보면 다음과 같다.

댓글