Skip to content

Commit

Permalink
Merge pull request #556 from phageghost/dont_assume_same_sample_sizes…
Browse files Browse the repository at this point in the history
…_in_prune

Replaced some unique() calls in power_prune() with mean()
  • Loading branch information
mightyphil2000 authored Sep 3, 2024
2 parents 7976c28 + d7ecf52 commit d70ec00
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 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 @@ -341,14 +339,15 @@ power_prune <- function(dat,method=1,dist.outcome="binary")
r2sum<-sum(r2) # sum of the r-squares for each SNP in the instrument
# F<-r2sum*(n-1-k)/((1-r2sum*k )
if(dist.outcome == "continuous"){
iv.se<- 1/sqrt(unique(dat2$samplesize.outcome)*r2sum) #standard error of the IV should be proportional to this
iv.se<- 1/sqrt(mean(dat2$samplesize.outcome)*r2sum) #standard error of the IV should be proportional to this
}
if(dist.outcome == "binary"){
iv.se<-1/sqrt(unique(n.cas)*unique(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(unique(dat2$samplesize.outcome)*r2sum)
}
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
}
}
# 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 d70ec00

Please sign in to comment.