36 lines
1.5 KiB
R
36 lines
1.5 KiB
R
## File Name: select_weight.R
|
|
## File version: 1.0
|
|
|
|
#' Compute confounding weights for the PCBSM.
|
|
#'
|
|
#' This function computes weights to be included in a PCBSM as a covariate accounting for unobserved confounding. Obtained by extracting response residuals from a probit model with grp as dependent variable and confounders and instruments as independent variables.
|
|
#'
|
|
#' @param df data.frame containing the data
|
|
#' @param grp string containing the name of the column where the group membership variable is stored in df
|
|
#' @param X vector of strings containing the name of confounders to be included in the model
|
|
#' @param instr vector of strings containing the name of instrumental variables to be included in the model
|
|
#' @param type.res string containing the type of glm residuals to be used. Default is "deviance"
|
|
#' @param std boolean indicating whether residuals should be standardized. Default is TRUE
|
|
#' @return A vector of weights to be included in a PCBSM
|
|
#' @export
|
|
|
|
|
|
select_weight <- function(df=NULL,grp=NULL,X=NULL,instr=NULL,type.res="deviance",std=F) {
|
|
formu <- paste0(grp,"~")
|
|
formu2 <- paste(X,sep="+",collapse="+")
|
|
formu3 <- paste(instr,sep="+",collapse="+")
|
|
if (!is.null(instr)) {
|
|
formu2 <- paste(formu2,formu3,sep="+")
|
|
}
|
|
if (is.null(X)) {
|
|
formu2 <- formu3
|
|
}
|
|
formu <- paste(formu,formu2)
|
|
logit_mod <- glm(formula = formu,data = df,family = binomial(link = "probit"))
|
|
res <- residuals(logit_mod,type=type.res)
|
|
if (std) {
|
|
res <- rstandard(logit_mod,type='deviance')
|
|
}
|
|
return(res)
|
|
}
|