将边际频率添加到三维频率表中。

huangapple go评论66阅读模式
英文:

add marginal frequency to three way frequency table

问题

我想创建一个包含三个(加权)变量和边际频率的交叉频率表。
这是一个可重现的示例:

library(stats)
library(dplyr)

data("cms_patient_care", package = "tidyr")
weights <- rnorm(252, 1, 0.1)
df <- cbind(cms_patient_care, weights)
crosstabs <- xtabs(weights ~ facility_name + type + measure_abbr, data = df)

现在我想要每个measure_abbrfacility_nametype的边际频率。

到目前为止,我已经使用以下函数获得了facility_nametype每个元素的边际频率,并使用addmargins()函数添加了边际频率:

marg <- function(x) {x[1] / sum(x) * 100}
crosstabs2 <- addmargins(crosstabs, margin = 3, FUN = marg)

我该如何获得其余部分?

英文:

I want to create a cross frequency table of three (weighted) variables with marginal frequency.
Here is a reproducible example

library(stats)
library(dplyr)

data(&quot;cms_patient_care&quot;, package = &quot;tidyr&quot;)
weights &lt;- rnorm(252, 1,0.1)
df &lt;- cbind(cms_patient_care, weights)
crosstabs &lt;- xtabs(weights ~ facility_name + type + measure_abbr, data=df)

Now I'd like to have the marginal frequency of each measure_abbr by facility_name * type

So far, I've managed to have the marginal frequency of the first element of each facility_name*type with this function and addmargins()

marg &lt;- function(x) {x[1]/sum(x)*100} 
crosstabs2 &lt;- addmargins(crosstabs, margin=3, FUN = marg)

How can I get the rest of it ?

答案1

得分: 0

如果有人想要做相同的事情,我已经放弃使用addmargins()的想法,因为输出必须与边际中指定的维度相同,所以为了使其工作,您需要执行以下操作,这实际上没有用:

marg <- function(x) {x[1]/sum(x)*100} 
marg2 <- function(x) {x[2]/sum(x)*100} 
marg3 <- function(x) {x[3]/sum(x)*100} 
...
crosstabs2 <- addmargins(crosstabs, margin=c(3,3,3,...), FUN = c(marg, marg2, marg3,...))

我已经成功使用一个小函数来完成相同的操作:

  • data是xtabs的输出
  • variable_name是要获取边际频率的变量名称(在引号内),在示例中为"measure_abbr"。请注意,我使用position来选择变量。
freq_marg <- function(data, variable_name){
  df <- as.data.frame(data) %>%  
    group_by(across(1:which(colnames(.) == variable_name) - 1)) %>%
    mutate(Somme_Freq = sum(Freq)) %>%
    ungroup() %>%
    mutate(RowPercent = Freq / Somme_Freq)
  return(df)
}
crosstabs2 <- freq_marg(crosstabs,"measure_abbr")
英文:

In case someone is looking to do the same, I've dropped the idea of using addmargins() because the output has to be the same dimension as stated in margin, so in order for it to work, you'd need to do the following, which is really unuseful

marg &lt;- function(x) {x[1]/sum(x)*100} 
marg2 &lt;- function(x) {x[2]/sum(x)*100} 
marg3 &lt;- function(x) {x[3]/sum(x)*100} 
...
crosstabs2 &lt;- addmargins(crosstabs, margin=c(3,3,3,...), FUN = c(marg, marg2, marg3,...))

I've managed to do the same using a small function:

  • data is the output of xtabs
  • variable_name is the name of the variable (between quotation marks) you want the marginal frequency of (in the example : "measure_abbr"). Note that I use position to select variables.
freq_marg &lt;- function(data, variable_name){
  df &lt;- as.data.frame(data) %&gt;%  
    group_by(across(1:which(colnames(.) == variable_name) - 1)) %&gt;%
    mutate(Somme_Freq = sum(Freq)) %&gt;%
    ungroup() %&gt;%
    mutate(RowPercent = Freq / Somme_Freq)
  return(df)
}
crosstabs2 &lt;- freq_marg(crosstabs,&quot;measure_abbr&quot;)

huangapple
  • 本文由 发表于 2023年7月13日 21:10:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定