adding pvals to strobe_diff_bygroup

This commit is contained in:
agdamsbo 2019-02-03 12:14:29 +01:00
parent 0f1c0cfe17
commit 0819d48953
3 changed files with 111 additions and 81 deletions

View File

@ -1,7 +1,7 @@
Package: daDoctoR Package: daDoctoR
Type: Package Type: Package
Title: FUNCTIONS FOR HEALTH RESEARCH Title: FUNCTIONS FOR HEALTH RESEARCH
Version: 0.1.0.9019 Version: 0.1.0.9020
Author@R: c(person("Andreas", "Gammelgaard Damsbo", email = "agdamsbo@pm.me", role = c("cre", "aut"))) Author@R: c(person("Andreas", "Gammelgaard Damsbo", email = "agdamsbo@pm.me", role = c("cre", "aut")))
Maintainer: Andreas Gammelgaard Damsbo <agdamsbo@pm.me> Maintainer: Andreas Gammelgaard Damsbo <agdamsbo@pm.me>
Description: I am a Danish medical doctor involved in neuropsychiatric research. Description: I am a Danish medical doctor involved in neuropsychiatric research.
@ -15,4 +15,4 @@ Imports: broom,
License: GPL (>= 2) License: GPL (>= 2)
Encoding: UTF-8 Encoding: UTF-8
LazyData: true LazyData: true
RoxygenNote: 6.1.0.9000 RoxygenNote: 6.1.1

View File

@ -6,107 +6,137 @@
#' @param groups groups to compare, as string. #' @param groups groups to compare, as string.
#' @param adj variables to adjust for, as string. #' @param adj variables to adjust for, as string.
#' @param data dataframe of data. #' @param data dataframe of data.
#' @param dec decimals for results, standard is set to 2. Mean and sd is dec-1. #' @param dec decimals for results, standard is set to 2. Mean and sd is dec-1. pval has 3 decimals.
#' @keywords strobe #' @keywords strobe
#' @export #' @export
#' @examples #' @examples
#' strobe_diff_bygroup() #' strobe_diff_bygroup()
strobe_diff_bygroup<-function(meas,var,group,adj,data,dec=2){ strobe_diff_bygroup<-function(meas,var,group,adj,data,dec=2){
## Wishlist:
## -fix confint()
## meas: sdmt ## meas: sdmt
## var: rtreat ## var: rtreat
## group: genotype ## group: genotype
## for dichotome exposure variable (var) ## for dichotome exposure variable (var)
d<-data d <- data
m<-d[,c(meas)] m <- d[, c(meas)]
v<-d[,c(var)] v <- d[, c(var)]
g<-d[,c(group)] g <- d[, c(group)]
ads <- d[, c(adj)]
dat <- data.frame(m, v, g, ads)
df <- data.frame(matrix(ncol = 9))
if (!is.factor(m)) {
for (i in 1:length(levels(g))) {
grp <- levels(dat$g)[i]
di <- dat[dat$g == grp, ][, -3]
mod <- lm(m ~ v, data = di)
ads<-d[,c(adj)] p <- coef(summary(mod))[2,4]
p<-ifelse(p<0.001,"<0.001",round(p,3))
p <- ifelse(p<=0.05|p=="<0.001",paste0("*",p),
ifelse(p>0.05&p<=0.1,paste0(".",p),p))
pv<-p
dat<-data.frame(m,v,g,ads) co<-round(coef(mod),dec)[2]
ci<-round(confint(mod),dec)[2,]
lo<-ci[1]
up<-ci[2]
ci<-paste0(co," (",lo," to ",up,")")
df<-data.frame(matrix(ncol=7)) amod <- lm(m ~ ., data = di)
pa <- coef(summary(amod))[2,4]
pa<-ifelse(pa<0.001,"<0.001",round(pa,3))
pa <- ifelse(pa<=0.05|pa=="<0.001",paste0("*",pa),
ifelse(pa>0.05&pa<=0.1,paste0(".",pa),pa))
apv<-pa
if(!is.factor(m)){ aco<-round(coef(amod),dec)[2]
aci<-round(confint(amod),dec)[2,]
alo<-aci[1]
aup<-aci[2]
aci<-paste0(aco," (",alo," to ",aup,")")
for (i in 1:length(levels(g))){ nr <- c()
grp<-levels(dat$g)[i] for (r in 1:2) {
di<-dat[dat$g==grp,][,-3] vr <- levels(di$v)[r]
dr <- di[di$v == vr, ]
mod<-lm(m~v,data=di) n <- as.numeric(nrow(dr[!is.na(dr$m), ]))
co<-round(coef(mod)[-1],dec) mean <- round(mean(dr$m, na.rm = TRUE), dec -
lo<-round(confint(mod)[-1,1],dec) 1)
up<-round(confint(mod)[-1,2],dec) sd <- round(sd(dr$m, na.rm = TRUE), dec - 1)
ms <- paste0(mean, " (", sd, ")")
ci<-paste0(co," (",lo," to ",up,")") nr <- c(nr, n, ms)
}
amod<-lm(m~.,data=di) irl <- c(grp, nr, ci, pv, aci, apv)
aco<-round(coef(amod)[2],dec) df <- rbind(df, irl)
alo<-round(confint(amod)[2,1],dec) names(df) <- c("grp",
aup<-round(confint(amod)[2,2],dec) paste0("N.", substr(levels(v)[1], 1, 3)),
paste0("M.", substr(levels(v)[1], 1, 3)),
aci<-paste0(aco," (",alo," to ",aup,")") paste0("N.", substr(levels(v)[2], 1, 3)),
paste0("M.", substr(levels(v)[2], 1, 3)),
nr<-c() "diff",
"pval",
for (r in 1:2){ "ad.diff",
vr<-levels(di$v)[r] "ad.pval")
dr<-di[di$v==vr,]
n<-as.numeric(nrow(dr[!is.na(dr$m),]))
mean<-round(mean(dr$m,na.rm = TRUE),dec-1)
sd<-round(sd(dr$m,na.rm = TRUE),dec-1)
ms<-paste0(mean," (",sd,")")
nr<-c(nr,n,ms)
} }
irl<-c(grp,nr,ci,aci) }
df<-rbind(df,irl) if (is.factor(m)) {
names(df)<-c("grp",paste0("N.",substr(levels(v)[1],1,3)),paste0("M.",substr(levels(v)[1],1,3)),paste0("N.",substr(levels(v)[2],1,3)),paste0("M.",substr(levels(v)[2],1,3)),"diff","ad.diff") for (i in 1:length(levels(g))) {
}} grp <- levels(dat$g)[i]
di <- dat[dat$g == grp, ][, -3]
if(is.factor(m)){ mod <- glm(m ~ v, family = binomial(), data = di)
for (i in 1:length(levels(g))){ p <- coef(summary(mod))[2,4]
grp<-levels(dat$g)[i] p<-ifelse(p<0.001,"<0.001",round(p,3))
di<-dat[dat$g==grp,][,-3] p <- ifelse(p<=0.05|p=="<0.001",paste0("*",p),
ifelse(p>0.05&p<=0.1,paste0(".",p),p))
pv<-p
mod<-glm(m~v,family=binomial(),data=di) co <- round(exp(coef(mod)[-1]), dec)
co<-round(exp(coef(mod)[-1]),dec) ci<-round(exp(confint(mod)),dec)[2,]
lo<-round(exp(confint(mod)[-1,1]),dec) lo<-ci[1]
up<-round(exp(confint(mod)[-1,2]),dec) up<-ci[2]
ci <- paste0(co, " (", lo, " to ", up, ")")
ci<-paste0(co," (",lo," to ",up,")") amod <- glm(m ~ ., family = binomial(), data = di)
amod<-glm(m~.,family=binomial(),data=di) pa <- coef(summary(amod))[2,4]
aco<-round(exp(coef(amod)[2]),dec) pa<-ifelse(pa<0.001,"<0.001",round(pa,3))
alo<-suppressMessages(round(exp(confint(amod)[2,1]),dec)) pa <- ifelse(pa<=0.05|pa=="<0.001",paste0("*",pa),
aup<-suppressMessages(round(exp(confint(amod)[2,2]),dec)) ifelse(pa>0.05&pa<=0.1,paste0(".",pa),pa))
apv<-pa
aci<-paste0(aco," (",alo," to ",aup,")") aco <- round(exp(coef(amod)[2]), dec)
aci<-suppressMessages(round(exp(confint(amod)),dec))[2,]
alo<-aci[1]
aup<-aci[2]
aci <- paste0(aco, " (", alo, " to ", aup, ")")
nr <- c()
nr<-c() for (r in 1:2) {
vr <- levels(di$v)[r]
for (r in 1:2){ dr <- di[di$v == vr, ]
vr<-levels(di$v)[r] n <- as.numeric(nrow(dr[!is.na(dr$m), ]))
dr<-di[di$v==vr,] nl <- levels(m)[2]
n<-as.numeric(nrow(dr[!is.na(dr$m),])) out <- nrow(dr[dr$m == nl & !is.na(dr$m), ])
nl<-levels(m)[2] pro <- round(out/n * 100, 0)
out<-nrow(dr[dr$m==nl&!is.na(dr$m),]) rt <- paste0(out, " (", pro, "%)")
pro<-round(out/n*100,0) nr <- c(nr, n, rt)
rt<-paste0(out," (",pro,"%)") }
irl <- c(grp, nr, ci, pv, aci, apv)
nr<-c(nr,n,rt) df <- rbind(df, irl)
names(df) <- c("grp",
paste0("N.", substr(levels(v)[1], 1, 3)),
paste0(nl, ".", substr(levels(v)[1], 1, 3)),
paste0("N.", substr(levels(v)[2], 1, 3)),
paste0(nl, ".", substr(levels(v)[2], 1, 3)),
"OR",
"pval",
"ad.OR",
"ad.pval")
} }
irl<-c(grp,nr,ci,aci) }
df<-rbind(df,irl) return(df)
names(df)<-c("grp",paste0("N.",substr(levels(v)[1],1,3)),paste0(nl,".",substr(levels(v)[1],1,3)),paste0("N.",substr(levels(v)[2],1,3)),paste0(nl,".",substr(levels(v)[2],1,3)),"OR","ad.OR") }
}}
return(df)
}

View File

@ -15,7 +15,7 @@ strobe_diff_bygroup(meas, var, group, adj, data, dec = 2)
\item{data}{dataframe of data.} \item{data}{dataframe of data.}
\item{dec}{decimals for results, standard is set to 2. Mean and sd is dec-1.} \item{dec}{decimals for results, standard is set to 2. Mean and sd is dec-1. pval has 3 decimals.}
\item{groups}{groups to compare, as string.} \item{groups}{groups to compare, as string.}
} }