Simulate data

n <- 10000
geno <- rbinom(n, 2, 0.3)

u <- rnorm(n)
x <- geno + rnorm(n) + u
y <- u + 3 * x + rnorm(n)

estimate the effects of the snp on exposuyre and outcome

mod1 <- summary(lm(x ~ geno))
bgx <- coefficients(mod1)[2,1]
segx <- coefficients(mod1)[2,2]

mod2 <- summary(lm(y ~ geno))
bgy <- coefficients(mod2)[2,1]
segy <- coefficients(mod2)[2,2]

Compare observational

# obs association
lm(y ~ x)
## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##     -0.2283       3.3993

against IV

# iv estimate
bgy / bgx
## [1] 2.950444

Using the TwoSampleMR package

library(TwoSampleMR)
## TwoSampleMR version 0.5.5 
## [>] New: Option to use non-European LD reference panels for clumping etc
## [>] Some studies temporarily quarantined to verify effect allele
## [>] See news(package='TwoSampleMR') and https://gwas.mrcieu.ac.uk for further details
# using twosamplemr to get se
mr_wald_ratio(bgx, bgy, segx, segy)
## $b
## [1] 2.950444
## 
## $se
## [1] 0.07751368
## 
## $pval
## [1] 0
## 
## $nsnp
## [1] 1