Skip to content

Commit

Permalink
Check for N/As before taking mean of n.cas and n.con
Browse files Browse the repository at this point in the history
  • Loading branch information
phageghost committed Aug 30, 2024
1 parent 0a0c75f commit d7ecf52
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions R/format_mr_results2.R
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,6 @@ power_prune <- function(dat,method=1,dist.outcome="binary")
id.set.unique<-unique(id.sets)
dat$id.set<-as.numeric(factor(id.sets))
for(i in seq_along(id.set.unique)){
print(i)
print(id.set.unique[i])
dat1<-dat[id.sets == id.set.unique[i],]
# unique(dat1[,c("exposure","outcome")])
id.subset<-paste(dat1$exposure,dat1$id.exposure,dat1$outcome,dat1$id.outcome)
Expand Down Expand Up @@ -344,11 +342,12 @@ power_prune <- function(dat,method=1,dist.outcome="binary")
iv.se<- 1/sqrt(mean(dat2$samplesize.outcome)*r2sum) #standard error of the IV should be proportional to this

Check warning on line 342 in R/format_mr_results2.R

View check run for this annotation

Codecov / codecov/patch

R/format_mr_results2.R#L342

Added line #L342 was not covered by tests
}
if(dist.outcome == "binary"){
iv.se<-1/sqrt(mean(n.cas)*mean(n.con)*r2sum) #standard error of the IV should be proportional to this
if(any(is.na(n.cas)) || any(is.na(n.con))) {
warning("dist.outcome set to binary but number of cases or controls is missing. Will try using total sample size instead but power pruning will be less accurate")
iv.se<- 1/sqrt(mean(dat2$samplesize.outcome)*r2sum)
}
} else {
iv.se<-1/sqrt(mean(n.cas)*mean(n.con)*r2sum) #standard error of the IV should be proportional to this
}

Check warning on line 350 in R/format_mr_results2.R

View check run for this annotation

Codecov / codecov/patch

R/format_mr_results2.R#L347-L350

Added lines #L347 - L350 were not covered by tests
}
# Power calculations to implement at some point
# iv.se<-1/sqrt(unique(n.cas)*unique(n.con)*r2sum) #standard error of the IV should be proportional to this
Expand Down

0 comments on commit d7ecf52

Please sign in to comment.