2014-11-13 1 views
3

고품질 회귀 테이블을 만들기 위해 R 패키지 stargazer을 사용하고 있습니다. 요약 통계 테이블을 만들 때 사용하고 싶습니다. 데이터에 요인 변수가 있고 요약 표에 요인의 각 범주에 대한 백분율이 표시됩니다. 사실상 요인을 상호 배타적 논리 집합 (더미) 변수 집합으로 분리 한 다음 표시합니다. 테이블에있는 사람들.몽상가 요약 통계 테이블에서 각 요인 수준을 더미 변수로 출력합니다.

> library(car) 
> library(stargazer) 
> data(Blackmoor) 
> stargazer(Blackmoor[, c("age", "exercise", "group")], type = "text") 

========================================== 
Statistic N Mean St. Dev. Min Max 
------------------------------------------ 
age  945 11.442 2.766 8.000 17.920 
exercise 945 2.531 3.495 0.000 29.960 
------------------------------------------ 

그러나 나는 (이 데이터 % 제어 및/또는 % 환자) 나에게 각 그룹의 퍼센트를 보여줍니다 추가 행을 얻으려고 예를 들면 다음과 같습니다이다. 나는 이것이 실무자 어딘가의 선택 일 뿐이라고 확신하지만, 나는 그것을 발견 할 수 없다. 누구는 그것이 무엇인지 압니까?

+0

몽 상가가 자동으로이 작업을 수행 할 수 없습니다. 이 질문도 참조하십시오 : http://stackoverflow.com/questions/25473689/summarizing-factors-and-times-with-stargazer – Andrew

+0

하지만 요약 테이블을 만든 다음 pander 또는 xtable을 사용하여 Markdown, Word로 변환 할 수 있습니다 , LaTeX, HTML 또는 기타 원하는 것. – Andrew

+0

감사합니다. 아직 옵션이 없다는 것은 너무 나빴습니다. 귀하의 해결 방법은 내가 찾고있는 것과 가깝지만, 통제 상태에서는 %로, 환자 상태에서는 %로하고 싶었습니다. 내 해결 방법도 게시 할 예정입니다. –

답변

4

Stargazer는이를 직접 수행 할 수 없으므로 사용자 자신의 요약 테이블을 데이터 프레임으로 만들고 pander, xtable 또는 다른 패키지를 사용하여 출력 할 수 있습니다. 예를 들어, 다음은 요약 테이블을 작성 dplyr 및 tidyr를 사용하는 방법은 다음과 같습니다

당신이 판더를 사용하는 경우이 결과
library(dplyr) 
library(tidyr) 

fancy.summary <- Blackmoor %>% 
    select(-subject) %>% # Remove the subject column 
    group_by(group) %>% # Group by patient and control 
    summarise_each(funs(mean, sd, min, max, length)) %>% # Calculate summary statistics for each group 
    mutate(prop = age_length/sum(age_length)) %>% # Calculate proportion 
    gather(variable, value, -group, -prop) %>% # Convert to long 
    separate(variable, c("variable", "statistic")) %>% # Split variable column 
    mutate(statistic = ifelse(statistic == "length", "n", statistic)) %>% 
    spread(statistic, value) %>% # Make the statistics be actual columns 
    select(group, variable, n, mean, sd, min, max, prop) # Reorder columns 

:

library(pander) 

pandoc.table(fancy.summary) 

------------------------------------------------------ 
group variable n mean sd min max prop 
------- ---------- --- ------ ----- ----- ----- ------ 
control age  359 11.26 2.698 8 17.92 0.3799 

control exercise 359 1.641 1.813 0 11.54 0.3799 

patient age  586 11.55 2.802 8 17.92 0.6201 

patient exercise 586 3.076 4.113 0 29.96 0.6201 
------------------------------------------------------ 
1

다른 해결 방법은 더미 변수를 만들 수 model.matrix을 사용하는 것입니다 별도의 단계를 거친 다음 stargazer을 사용하여 테이블을 만듭니다. 예 이것을 표시하려면 :

> library(car) 
> library(stargazer) 
> data(Blackmoor) 
> 
> options(na.action = "na.pass") # so that we keep missing values in the data 
> X <- model.matrix(~ age + exercise + group - 1, data = Blackmoor) 
> X.df <- data.frame(X) # stargazer only does summary tables of data.frame objects 
> names(X) <- colnames(X) 
> stargazer(X.df, type = "text") 

============================================= 
Statistic  N Mean St. Dev. Min Max 
--------------------------------------------- 
age   945 11.442 2.766 8.000 17.920 
exercise  945 2.531 3.495 0.000 29.960 
groupcontrol 945 0.380 0.486  0  1 
grouppatient 945 0.620 0.486  0  1 
--------------------------------------------- 
1

패키지 tables이 작업에 유용 할 수 있습니다.

library(car) 
library(tables) 
data(Blackmore) 

# percent only: 
(x <- tabular((Factor(group, "")) ~ (Pct=Percent()) * Format(digits=4), 
    data=Blackmore)) 
##    
##   Pct 
## control 37.99 
## patient 62.01 

# percent and counts: 
(x <- tabular((Factor(group, "")) ~ ((n=1) + (Pct=Percent())) * Format(digits=4), 
    data=Blackmore)) 
##      
##   n  Pct 
## control 359.00 37.99 
## patient 586.00 62.01 

는 그럼 LaTeX의 출력이에 간단합니다 :

> latex(x) 
\begin{tabular}{lcc} 
\hline 
    & n & \multicolumn{1}{c}{Pct} \\ 
\hline 
control & $359.00$ & $\phantom{0}37.99$ \\ 
patient & $586.00$ & $\phantom{0}62.01$ \\ 
\hline 
\end{tabular}