-
Notifications
You must be signed in to change notification settings - Fork 0
/
appliedTest.jl
78 lines (63 loc) · 2.26 KB
/
appliedTest.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using RCall
# R packages for generation of table using GT
R"""
library(gt)
library(magrittr)
library(tibble)
library(tidyr)
library(dplyr)
library(reshape2)
"""
include("semiParam3.jl")
#########################################################################
# APPLIED STUDY
#########################################################################
# Load the data in
root = dirname(@__FILE__)
DF = CSV.read(root * "/exercise3data.csv", DataFrame, header = [:ID, :Y, :R, :X], skipto = 2)
#########################################################################
# Produce dataframe
#########################################################################
# Do a GLM fit of the naive estimator
fitwithout = glm(@formula(Y ~ R), DF, Binomial(), LogitLink())
coefGLM = coef(fitwithout)[2]
confintGLMLower, confintGLMUpper = confint(fitwithout)[2, :]
# Compute the efficient estimate
βhatEff, seβhatEff = effEst(DF.R, DF.Y, DF.X)
confintHatEffLower, confintHatEffUpper = quantile.(Normal(βhatEff,seβhatEff), [0.025, 0.975])
# Compute the polynomial estimator
βtilde, seβtilde = polEst(DF.R, DF.Y, DF.X)
confintPolLower, confintPolUpper = quantile.(Normal(βtilde,seβtilde), [0.025, 0.975])
# Collect estimates into dataframe
DFest = DataFrame(
type = ["Naive", "Efficient", "Polynomial"],
estimate = [coefGLM, βhatEff, βtilde],
lower = [confintGLMLower, confintHatEffLower,confintPolLower],
upper = [confintGLMUpper, confintHatEffUpper,confintPolUpper]
)
# get odds ratios
#exp.(select(DFest, [:estimate, :lower, :upper]))
#########################################################################
# Produce tables to latex using GT in R
#########################################################################
# Get path of current file
root = dirname(@__FILE__)
# Put the full dataframe and path to R using RCall.jl
@rput DFest
@rput root
# Output the latex file with table code
R"""
tibble(DFest) |>
gt() |>
fmt_number(
columns = 2:4,
decimals = 3
) |>
tab_spanner(label = "confidence interval", columns = c("lower", "upper")
) |>
as_latex() |>
as.character() |>
writeLines(con = paste(root, "/latexApplied.tex", sep = ""))
"""
# Print table
pretty_table(DFest, formatters = ft_printf("%5.3f"), alignment = :c)