微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

计算比例报告比率相当于 RR和 Chi2 的函数中的错误

如何解决计算比例报告比率相当于 RR和 Chi2 的函数中的错误

我想创建一个函数来计算比例报告比率(相当于相对风险)和 Chi2 (researchgate.net/figure/Formulas-for-calculation-and-evaluation-of-the-proportional-reporting-ratio -PRR-AE_fig1_7254637) 来自如下所示的数据帧:

IDentifiantDuMessage CodeMedDRA 代码ATC 日期
1 AA1 3MMC 2018-01-04
2 BB2 1LSD 2018-02-04
dispropo <- function(test,patho,medoc){
    cpa = seq(min(ymd(test$Date)),max(ymd(test$Date)),by="weeks")
    cpa <- data.frame(Dates = cpa,Week = format(cpa,format = "%W"))
    cpa$Dates = format(cpa$Dates,format = "%Y")
    names(cpa)= c("year","week")
    test = test %>% dplyr::select(CodeMedDRA,CodeATC,Date,IdentifiantDuMessage)
    test$week = strftime(test$Date,format = "%V")
    test$year = strftime(test$Date,format = "%Y")
    test$range = 1:nrow(test)
    test = test %>% dplyr::select(range,year,week,CodeMedDRA,IdentifiantDuMessage)
    df <- data.frame(Week=character(),A=int(),B=int(),C=int(),D=int(),stringsAsFactors=FALSE)
    for (w in unique(test$week)) {
        test2 = test %>% filter(week == w)
        print(w)
        a = 0
        b = 0
        c = 0
        d = 0
        for (i in 1:nrow(test2)) {
            if (test2$CodeMedDRA[i] == patho & test2$CodeATC[i] == medoc ){
                a = a +1
            }
            
            else {
                if (test2$CodeMedDRA[i] == patho & test2$CodeATC[i] != medoc ) {
                    c = c+1
                }
                else {
                    if (test2$CodeMedDRA[i] != patho & test2$CodeATC[i] == medoc )
                    {
                        b = b + 1
                    }
                    else {
                        d = d+1
                    }
                }
            }
        }
        PRR=(a/(a+c))/(b/(b+d))
        x=c(a,b)
        y=c(c,d)
        chi2=chisq.tes(x,y,correct= TRUE)
        row<-data.frame(w,medoc,a,b,c,d,PRR,chi2$statistic)
        names(row) = c("Week","EI","Medicament","A","B","C","D","PRR","Chi2")
        df <- rbind(df,row)
    }
    return(df)
}

当我运行代码时,我收到以下错误消息: if (test2$CodeMedDRA[i] != patho & test2$CodeATC[i] == medoc) { 中的错误: valeur manquante là où TRUE / FALSE est requis

感谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。