Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wxwx1993 committed Jan 14, 2020
0 parents commit f870f3a
Show file tree
Hide file tree
Showing 113 changed files with 2,598 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
2 changes: 2 additions & 0 deletions .Rproj.user/D4EE1C3C/cpp-definition-cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[
]
6 changes: 6 additions & 0 deletions .Rproj.user/D4EE1C3C/pcs/debug-breakpoints.pper
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"debugBreakpointsState" : {
"breakpoints" : [
]
}
}
9 changes: 9 additions & 0 deletions .Rproj.user/D4EE1C3C/pcs/files-pane.pper
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"path" : "~/Dropbox/continuous GPS/GPSmatching/R",
"sortOrder" : [
{
"ascending" : true,
"columnIndex" : 2
}
]
}
3 changes: 3 additions & 0 deletions .Rproj.user/D4EE1C3C/pcs/source-pane.pper
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"activeTab" : 2
}
14 changes: 14 additions & 0 deletions .Rproj.user/D4EE1C3C/pcs/windowlayoutstate.pper
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"left" : {
"panelheight" : 862,
"splitterpos" : 361,
"topwindowstate" : "NORMAL",
"windowheight" : 903
},
"right" : {
"panelheight" : 862,
"splitterpos" : 541,
"topwindowstate" : "NORMAL",
"windowheight" : 903
}
}
6 changes: 6 additions & 0 deletions .Rproj.user/D4EE1C3C/pcs/workbench-pane.pper
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"TabSet1" : 3,
"TabSet2" : 3,
"TabZoom" : {
}
}
8 changes: 8 additions & 0 deletions .Rproj.user/D4EE1C3C/persistent-state
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
build-last-errors="[]"
build-last-errors-base-dir="~/Dropbox/continuous GPS/GPSmatching/"
build-last-outputs="[{\"output\":\"==> R CMD INSTALL --preclean --no-multiarch --with-keep.source GPSmatching\\n\\n\",\"type\":0},{\"output\":\"* installing to library ‘/Library/Frameworks/R.framework/Versions/3.5/Resources/library’\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"* installing *source* package ‘GPSmatching’ ...\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** R\\n\",\"type\":1},{\"output\":\"** byte-compile and prepare package for lazy loading\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** help\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"*** installing help indices\\n\",\"type\":1},{\"output\":\"** building package indices\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"** testing if installed package can be loaded\\n\",\"type\":1},{\"output\":\"\",\"type\":1},{\"output\":\"* DONE (GPSmatching)\\n\",\"type\":1},{\"output\":\"\",\"type\":1}]"
compile_pdf_state="{\"errors\":[],\"output\":\"\",\"running\":false,\"tab_visible\":false,\"target_file\":\"\"}"
files.monitored-path=""
find-in-files-state="{\"handle\":\"\",\"input\":\"\",\"path\":\"\",\"regex\":true,\"results\":{\"file\":[],\"line\":[],\"lineValue\":[],\"matchOff\":[],\"matchOn\":[]},\"running\":false}"
imageDirtyState="1"
saveActionState="-1"
5 changes: 5 additions & 0 deletions .Rproj.user/D4EE1C3C/rmd-outputs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@





1 change: 1 addition & 0 deletions .Rproj.user/D4EE1C3C/saved_source_markers
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"active_set":"","sets":[]}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/10859896
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "7,1",
"scrollLine" : "0"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/22C1E8DB
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "6,16",
"scrollLine" : "0"
}
2 changes: 2 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/2603739E
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/2A3EF788
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "16,0",
"scrollLine" : "0"
}
2 changes: 2 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/2B5B49F0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/337CD24F
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "26,51",
"scrollLine" : "9"
}
2 changes: 2 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/3A8D2CFC
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/3C0F604E
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "5,0",
"scrollLine" : "0"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/3E36F6D7
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "3,25",
"scrollLine" : "0"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/44BB7997
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "16,4",
"scrollLine" : "5"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/6547FC1E
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "14,17",
"scrollLine" : "0"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/81C40F4B
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "4,0",
"scrollLine" : "0"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/93AC7835
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "36,91",
"scrollLine" : "29"
}
5 changes: 5 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/95C028E1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cursorPosition" : "16,22",
"scrollLine" : "4",
"tempName" : "Untitled1"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/9C407BEA
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "10,32",
"scrollLine" : "5"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/B3B4CE9F
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "30,1",
"scrollLine" : "16"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/C0F9D2EC
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "0,77",
"scrollLine" : "0"
}
5 changes: 5 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/C315FAD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cursorPosition" : "13,12",
"scrollLine" : "0",
"tempName" : "Untitled1"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/C3A20D2A
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "9,13",
"scrollLine" : "0"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/D21B565D
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "8,0",
"scrollLine" : "0"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/D48D6DC5
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "11,10",
"scrollLine" : "0"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/D760D727
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "48,0",
"scrollLine" : "24"
}
4 changes: 4 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/F0B69EF1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cursorPosition" : "4,12",
"scrollLine" : "0"
}
23 changes: 23 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/prop/INDEX
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
~%2FDesktop%2FGPSmatching-package.Rd="2B5B49F0"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2FDESCRIPTION="D21B565D"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2FNAMESPACE="81C40F4B"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2FR%2FCovariate_balance.R="C0F9D2EC"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2FR%2FGPSmatching-package.R="6547FC1E"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2FR%2FGPSmatching.R="D48D6DC5"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2FR%2FGPSmatching_smooth.R="3E36F6D7"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2FR%2FoptimIA-package.R="3A8D2CFC"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2Fman%2FGPSmatching-package.Rd="C315FAD"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2Fman%2FGPSmatching.Rd="95C028E1"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2Fman%2FGPSmatching_smooth.Rd="9C407BEA"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2Fman%2Fabsolute_corr_fun.Rd="2A3EF788"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2Fman%2Fcreate_matching.Rd="F0B69EF1"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2Fman%2Fmatching_l1.Rd="B3B4CE9F"
~%2FDropbox%2Fcontinuous%20GPS%2FGPSmatching%2Fman%2Fmatching_smooth.Rd="10859896"
~%2FDropbox%2Fcontinuous%20GPS%2FResubmission%2FApplication%2FCovariate_balance.R="93AC7835"
~%2FDropbox%2Fcontinuous%20GPS%2FResubmission%2FCovariate_balance.R="44BB7997"
~%2FDropbox%2Fcontinuous%20GPS%2FResubmission%2FGPSmatching.R="22C1E8DB"
~%2FDropbox%2Fcontinuous%20GPS%2FResubmission%2FOdessey%2FBenchmark200.R="337CD24F"
~%2FDropbox%2Fcontinuous%20GPS%2FResubmission%2FOdessey%2Fbalance1000.R="3C0F604E"
~%2FDropbox%2Fcontinuous%20GPS%2FResubmission%2FOdessey%2Fbalance200.R="C3A20D2A"
~%2FDropbox%2Fcontinuous%20GPS%2FResubmission%2FTest_balance.R="2603739E"
~%2FDropbox%2Fcontinuous%20GPS%2FResubmission%2Fdata_generate.R="D760D727"
Empty file.
54 changes: 54 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/s-2EABFB91/176BF097-contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
library("wCorr")
library("parallel")
library(fst)
library(data.table)
library("xgboost")
load("/nfs/home/X/xwu/shared_space/ci3_xwu/National_Causal/data2016/balance/covariates.RData")


cor_origin<-c(abs(cor(prematch_data$treat,prematch_data$year,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$mean_bmi,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$smoke_rate,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$hispanic,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$pct_blk,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$medhouseholdincome,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$medianhousevalue,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$poverty,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$education,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$popdensity,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$pct_owner_occ,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$pct_female,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$pct_dual,method = c("spearman"))),
abs(cor(prematch_data$treat,prematch_data$mean_entry_age,method = c("spearman"))))

cor_matched<-c(abs(cor(match_data$treat,match_data$year,method = c("spearman"))),
abs(cor(match_data$treat,match_data$mean_bmi,method = c("spearman"))),
abs(cor(match_data$treat,match_data$smoke_rate,method = c("spearman"))),
abs(cor(match_data$treat,match_data$hispanic,method = c("spearman"))),
abs(cor(match_data$treat,match_data$pct_blk,method = c("spearman"))),
abs(cor(match_data$treat,match_data$medhouseholdincome,method = c("spearman"))),
abs(cor(match_data$treat,match_data$medianhousevalue,method = c("spearman"))),
abs(cor(match_data$treat,match_data$poverty,method = c("spearman"))),
abs(cor(match_data$treat,match_data$education,method = c("spearman"))),
abs(cor(match_data$treat,match_data$popdensity,method = c("spearman"))),
abs(cor(match_data$treat,match_data$pct_owner_occ,method = c("spearman"))),
abs(cor(match_data$treat,match_data$pct_female,method = c("spearman"))),
abs(cor(match_data$treat,match_data$pct_dual,method = c("spearman"))),
abs(cor(match_data$treat,match_data$mean_entry_age,method = c("spearman"))))
#match_data2 <- match_data_xgb[((nrow(match_data_xgb)/50)*(4-1)+1):((nrow(match_data_xgb)/50)*(30)),]
order<-sort(as.numeric(cor_origin), index.return =T)$ix
par(mar=c(7.1, 10.1, 2.1, 2.1))
#par(mar=c(8.1,4.1,4.1,4.1))
par(mgp=c(5,1,0))
histcolors = c(rgb(0,0,1,.8), rgb(0,1,0,.8),rgb(1,0,0,.5),rgb(0.0,0.8,0,.5))
plot((as.numeric(cor_origin))[order],c(1:length(order)),yaxt="n",xlab="Absolute Correlation",ylab="",type = "b",lty=2,pch=3,xlim=c(0,0.55), col = histcolors[1],main="",lwd=3,cex.lab=2.5,cex.axis=2,cex.main=3)
#lines((as.numeric(cor_origin2))[order],c(1:length(order)), col = histcolors[1],lwd=3,lty=2,pch=1)
lines((as.numeric(cor_matched))[order],c(1:length(order)), col = histcolors[2],lwd=3,lty=2,pch=1)
#lines((as.numeric(cor_matched2))[order],c(1:length(order)), col = histcolors[2],lwd=3,pch=1)
#lines(((cor_weight))[order],c(1:length(order)), col = histcolors[3],lty=2,lwd=3)
#lines(((cor_weight2))[order],c(1:length(order)), col = histcolors[3],lwd=3)
axis(2, at=c(1:length(order)), labels=c("year","mean_bmi","smoke_rate","hispanic","pct_blk","MdHsIcm","MdVIHs",
"poverty","education","popdensity","pct_owner_occ","pct_female","pct_dual","mean_entry_age")[order],cex.axis=1.5,las=1)
legend("bottomright",legend = c("original","matched (boost)"),
col = histcolors[c(1,2)], lty= c(1,2))
abline(v=0.1,lty=3,lwd=3)
24 changes: 24 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/s-2EABFB91/1CF0D10F-contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#' The 'GPSmatching' package.
#'
#' @description An R package for implementing matching on generalized propensity scores with continuous exposures.
#' @details We developed an innovative approach for estimating causal effects using observational data in settings with continuous exposures, and introduce a new framework for GPS caliper matching.
#'
#' @docType package
#' @name GPSmatching-package
#' @aliases GPSmatching
#' @author Xiao Wu
#' @import parallel
#' @import data.table
#' @import SuperLearner
#' @import xgboost
#' @import earth
#' @import ranger
#' @import gam
#' @import KernSmooth
#'
#' @references
#' Wu, X., Mealli, F., Kioumourtzoglou, M.A., Dominici, F. and Braun, D., 2018. Matching on generalized propensity scores with continuous exposures. arXiv preprint arXiv:1812.06575.
#'
#' Kennedy, E.H., Ma, Z., McHugh, M.D. and Small, D.S., 2017. Non‐parametric methods for doubly robust estimation of continuous treatment effects. Journal of the Royal Statistical Society: Series B (Statistical Methodology), 79(4), pp.1229-1245.
#'
NULL
41 changes: 41 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/s-2EABFB91/1EBD073B-contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#' Estimate smoothed exposure-response function (ERF).
#'
#' @param matched_Y a vector of outcome variable in matched set.
#' @param matched_w a vector of continuous exposure variable in matched set.
#' @param bw.seq a vector of bandwidth values (Default is seq(0.2,2,0.2)).
#' @param w.vals a vector of exposure levels that ERF curves was evaluated.
#' @return
#' \code{erf}: The function returns a vector saved the output values of exposure-response function (ERF) given input \code{w.vals}.
#' @export

# Fit non-parametric kernel smoothing on matched set
matching_smooth<-function(matched_Y,
matched_w,
bw.seq=seq(0.2,2,0.2),
w.vals){
## The specified Gaussian kernel
kern_fun <- function(t){ dnorm(t) }
w_fun <- function(bw){
w.avals <- NULL
for (w.val in w.vals){
w.std <- (matched_w-w.val)/bw
kern.std <- kern_fun(w.std)/bw
w.avals <- c(w.avals, mean(w.std^2*kern.std)*(kern(0)/bw) /
(mean(kern.std)*mean(w.std^2*kern.std)-mean(w.std*kern.std)^2))
}
return(w.avals/length(matched_w))
}
hatvals <- function(bw){approx(w.vals,w_fun(bw),xout=matched_w,rule=2)$y}
smooth_fun <- function(out,bw){
approx(locpoly(w,out,bandwidth=bw, gridsize=1000),xout=matched_w,rule=2)$y
}
##
risk_fun <- function(h){
hats <- hatvals(h); mean( ((matched_Y - smooth_fun(matched_Y,bw=h))/(1-hats))^2)
}
risk.val <- sapply(bw.seq, risk_fun)
h.opt <- bw.seq[which.min(risk.val)]

erf <- approx(locpoly(matched_w, matched_Y, bandwidth=h.opt), xout=w.vals)$y
return(erf)
}
41 changes: 41 additions & 0 deletions .Rproj.user/D4EE1C3C/sources/s-2EABFB91/1FD38C45-contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#' Estimate smoothed exposure-response function (ERF).
#'
#' @param matched_Y a vector of outcome variable in matched set.
#' @param matched_w a vector of continuous exposure variable in matched set.
#' @param bw.seq a vector of bandwidth values (Default is seq(0.2,2,0.2)).
#' @param w.vals a vector of exposure levels that ERF curves was evaluated.
#' @return
#' \code{erf}: The function returns a vector saved the output values of exposure-response function (ERF) given input \code{w.vals}.
#' @export

# Fit non-parametric kernel smoothing on matched set
matching_smooth<-function(matched_Y,
matched_w,
bw.seq=seq(0.2,2,0.2),
w.vals){
## The specified Gaussian kernel
kern_fun <- function(t){ dnorm(t) }
w_fun <- function(bw){
w.avals <- NULL
for (w.val in w.vals){
w.std <- (matched_w-w.val)/bw
kern.std <- kern_fun(w.std)/bw
w.avals <- c(w.avals, mean(w.std^2*kern.std)*(kern_fun(0)/bw) /
(mean(kern.std)*mean(w.std^2*kern.std)-mean(w.std*kern.std)^2))
}
return(w.avals/length(matched_w))
}
hatvals <- function(bw){approx(w.vals,w_fun(bw),xout=matched_w,rule=2)$y}
smooth_fun <- function(out,bw){
approx(locpoly(matched_w,out,bandwidth=bw, gridsize=1000),xout=matched_w,rule=2)$y
}
##
risk_fun <- function(h){
hats <- hatvals(h); mean( ((matched_Y - smooth_fun(matched_Y,bw=h))/(1-hats))^2)
}
risk.val <- sapply(bw.seq, risk_fun)
h.opt <- bw.seq[which.min(risk.val)]

erf <- approx(locpoly(matched_w, matched_Y, bandwidth=h.opt), xout=w.vals)$y
return(erf)
}
Loading

0 comments on commit f870f3a

Please sign in to comment.