我正在使用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
------------------------------------------
但我正在尝试获得额外的一行,它显示每个组中的百分比(这些数据中的% control和/或% patient )。我确信这只是stargazer中的一个选项,但我找不到它。有人知道这是什么吗?
编辑:car::Blackmoor
已将拼写更新为car::Blackmore
。
发布于 2014-11-14 00:38:24
由于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
如果您使用pander,这将导致以下结果:
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-15 01:07:11
另一种解决方法是在单独的步骤中使用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
。
发布于 2015-07-11 01:09:05
包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}
https://stackoverflow.com/questions/26912957
复制相似问题