6

R パッケージのstargazerを使用して高品質の回帰表を作成していますが、それを使用して要約統計表を作成したいと考えています。データに因子変数があり、サマリー テーブルに因子の各カテゴリのパーセントを表示したいと考えています。実際には、因子を相互に排他的な論理 (ダミー) 変数のセットに分けてから表示します。表にあるもの。次に例を示します。

> library(car)
> library(stargazer)
> data(Blackmore)
> stargazer(Blackmore[, 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
------------------------------------------

しかし、私は、各グループの割合 (これらのデータでは、% コントロールおよび/または % 患者) を示す追加の行を取得しようとしています。これはスターゲイザーのどこかのオプションに過ぎないと確信していますが、見つかりません。誰がそれが何であるか知っていますか?

編集:car::Blackmoorスペルを に更新しましたcar::Blackmore

4

4 に答える 4

5

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
------------------------------------------------------
于 2014-11-13T16:38:24.403 に答える
2

もう 1 つの回避策は、 を使用model.matrixして別のステップでダミー変数を作成し、 を使用stargazerしてそれからテーブルを作成することです。例でこれを示すには:

> library(car)
> library(stargazer)
> data(Blackmore)
> 
> options(na.action = "na.pass")  # so that we keep missing values in the data
> X <- model.matrix(~ age + exercise + group - 1, data = Blackmore)
> 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   
---------------------------------------------

編集:car::Blackmoorスペルを に更新しましたcar::Blackmore

于 2014-11-14T17:07:10.953 に答える