我正在计算某些利率的置信区间。我正在用蒂迪厄斯和墓志铭从拜尔的方法计算CI。
我几乎肯定做错了什么。
library (tidyverse)
library (epitools)
# here's my made up data
DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
"Mumps","Mumps","Mumps","Mumps","Mumps",
"Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
79,91,69,89,78,
71,69,95,61,87)
AREA =c("A", "B","C")
DATA = data.frame(DISEASE, YEAR, VALUE,AREA)
# this is a simplification, I have the population values in another table, which I've merged
# to give me the dataframe I then apply pois.byar to.
DATA$POPN = ifelse(DATA$AREA == "A",2.5,
ifelse(DATA$AREA == "B",3,
ifelse(DATA$AREA == "C",7,0)))
# this bit calculates the number of things per area
rates<-DATA%>%group_by(DISEASE,AREA,POPN)%>%
count(AREA)如果我想计算CI,我想这是可行的
rates<-DATA%>%group_by(DISEASE,AREA,POPN)%>%
count(AREA) %>%
mutate(pois.byar(rates$n,rates$POPN))但我得到
Error in mutate_impl(.data, dots) :
Evaluation error: arguments imply differing number of rows: 0, 1.然而,这样做是可行的:
pois.byar(rates$n,rates$POPN)这样说似乎很愚蠢:“将pois.byar函数的结果转换为数据格式,然后合并回原来的值”。我可能只是为了得到一些数据.我不想那样做。这不是做事情的正确方法。
感激地收到任何建议。我认为这是一个相当基本的问题。表明我不是坐着学习而是试着边做边做。
这是我想要的疾病年n年面积x率较低的conf.level
发布于 2019-03-23 22:27:16
我不清楚你的预期产出对我来说是什么。你的评论没有多大帮助。最好显式地包含您所提供的示例数据的预期输出。
这里的问题是data.frame。因此,为了使mutate能够使用pois.byvar的输出,我们需要将data.frame存储在list中。
下面是您代码的更整洁的版本
library(tidyverse)
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN)))这将创建一个列res,其中包含pois.byar的data.frame输出。
或者您希望unnest list列来将条目展开到不同的列中?
library(tidyverse)
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN))) %>%
unnest()
## A tibble: 9 x 10
## Groups: DISEASE, AREA, POPN [9]
# DISEASE AREA POPN n x pt rate lower upper conf.level
# <fct> <fct> <dbl> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Chicky Pox A 2.5 1 1 2.5 0.4 0.0363 1.86 0.95
#2 Chicky Pox B 3 2 2 3 0.667 0.133 2.14 0.95
#3 Chicky Pox C 7 2 2 7 0.286 0.0570 0.916 0.95
#4 Marco Polio A 2.5 2 2 2.5 0.8 0.160 2.56 0.95
#5 Marco Polio B 3 2 2 3 0.667 0.133 2.14 0.95
#6 Marco Polio C 7 1 1 7 0.143 0.0130 0.666 0.95
#7 Mumps A 2.5 2 2 2.5 0.8 0.160 2.56 0.95
#8 Mumps B 3 1 1 3 0.333 0.0302 1.55 0.95
#9 Mumps C 7 2 2 7 0.286 0.0570 0.916 0.95https://stackoverflow.com/questions/55318712
复制相似问题