简体   繁体   English

从Hmisc包的summary()生成的交叉表中的行百分比

[英]Row Percentages in crosstable generated from summary() from the Hmisc package

I have been trying to learn to use the summary()-function from the Hmisc-package to generate crosstables that include chisquared tests. 我一直在尝试学习使用Hmisc包中的summary()函数来生成包含卡方检验的交叉表。 With help from this board I'm almost there. 在该委员会的帮助下,我快到了。 I just can't figure out how to obtain row-percentages instead of column percentages. 我只是不知道如何获取行百分比而不是列百分比。

#Data:
v1 <- sample(letters[8:12],200,replace=TRUE)
v2 <- sample(letters[1:2],200,replace=TRUE)
month <- sample(month.name[7:9],200,replace=TRUE)
df <- data.frame(v1,v2,month)

#Table:
latex(    summary( month  ~ v1 + v2 , data=df,  method="reverse" ,test=TRUE),        exclude1=FALSE,file="",booktabs=TRUE,long=TRUE)

Which gets me this: 这让我这样: 在此处输入图片说明

This gets me the column-percentages. 这使我获得了列百分比。 I am looking for a way to turn it around so i get the row-percentages instead. 我正在寻找一种解决方法,以便获得行百分比。 I've been searching the Hmisc-documentation for "row" and "column" and "percent" but no luck. 我一直在Hmisc文档中搜索“行”,“列”和“百分比”,但是没有运气。 The summary.formular() function has the optional argument "fun" but it is over my head to get it to do row percentages... summary.formular()函数具有可选参数“ fun”,但是让我去做行百分比很麻烦……

Please Help 请帮忙

If you hack around a bit with the Hmisc::formatCats. 如果您使用Hmisc :: formatCats进行修改。 Namely, change the MARGIN from 2 to 1. You can get there. 即,将MARGIN从2更改为1。您可以到达那里。

Part of formatCats 格式猫的一部分

denom <- if (type == 1) apply(tab, 2, sum) else group.freq
pct <- 100 * (if (ncol(tab) > 1) sweep(tab, 2, denom, FUN = "/") else tab/denom)

Change to 改成

denom <- if (type == 1) apply(tab, 1, sum) else group.freq
pct <- 100 * (if (ncol(tab) > 1) sweep(tab, 1, denom, FUN = "/") else tab/denom)

I made a gist at https://gist.github.com/jwijffels/5599349 with this modified function called myformatCats. 我在https://gist.github.com/jwijffels/5599349上创建了要点,并带有名为myformatCats的已修改函数。 Get it, assign it in the Hmisc namespace to override Hmisc::formatCats and it prints out the col pct. 得到它,在Hmisc命名空间中分配它以覆盖Hmisc :: formatCats并打印出col pct。

require(Hmisc)
require(devtools)
source_gist("5599349")
assignInNamespace(x="formatCats", value=myformatCats, ns="Hmisc")

v1 <- sample(letters[8:12],200,replace=TRUE)
v2 <- sample(letters[1:2],200,replace=TRUE)
month <- sample(month.name[7:9],200,replace=TRUE)
df <- data.frame(v1,v2,month)
summary( month  ~ v1 + v2 , data=df,  method="reverse")

For some reason I could not add a comment. 由于某种原因,我无法添加评论。 I tried the solution by jwijjfels but it didn´t work. 我尝试过jwijjfels的解决方案,但是没有用。 Turns out Hmisc has changed so in Hmisc 3.14-3 you have to make the following changes: 事实证明Hmisc已更改,因此在Hmisc 3.14-3中,您必须进行以下更改:

Edit line 15-21 in Hmisc:::formatCats into the following and replace this function with formatCats as described by jwijffels. 将Hmisc ::: formatCats中的第15-21行编辑为以下内容,并用jwijffels描述的formatCats替换此函数。

denom <- if (type == 1) 
    apply(tab, 1, sum)
  else group.freq
pct <- if (ncol(tab) > 1) 
   sweep(tab, 1, denom, FUN = "/")
  else tab/denom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM