NHANES fecal incontinence code

Author

Sonia Bhatia

Published

December 15, 2023

knitr::opts_chunk$set(echo = TRUE)
options(repos = list(CRAN="http://cran.rstudio.com/"))

# Load survey and dplyr packages
#+ message = FALSE, warning=FALSE
#+ 
#install.packages("dplyr", repos = "http://cran.us.r-project.org")
library(dplyr)

#install.packages("survey", repos = "http://cran.us.r-project.org")
library(survey)
#install.packages("questionr")                                    # Install questionr package
library("questionr") 
#install.packages("tableone")
library(tableone)
#install.packages("caTools")
library(caTools)
library(tidyverse)

options(survey.lonely.psu='adjust')




#import diabetes data 2005-2010

# 2005-2006
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2005-2006/DIQ_D.XPT", tf <- tempfile(), mode="wb")
DIQ_D <- foreign::read.xport(tf)[,c("SEQN","DIQ010","DID040","DIQ050" )]

# 2007-2008
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2007-2008/DIQ_E.XPT", tf <- tempfile(), mode="wb")
DIQ_E <- foreign::read.xport(tf)[,c("SEQN","DIQ010","DID040","DIQ050")]

# 2009-2010
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2009-2010/DIQ_F.XPT", tf <- tempfile(), mode="wb")
DIQ_F <- foreign::read.xport(tf)[,c("SEQN","DIQ010","DID040","DIQ050")]


#import glycohemoglobin data 2005-2010

# 2005-2006
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2005-2006/GHB_D.XPT", tf <- tempfile(), mode="wb")
GHB_D <- foreign::read.xport(tf)[,c("SEQN","LBXGH")]

# 2007-2008
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2007-2008/GHB_E.XPT", tf <- tempfile(), mode="wb")
GHB_E <- foreign::read.xport(tf)[,c("SEQN","LBXGH")]

# 2009-2010
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2009-2010/GHB_F.XPT", tf <- tempfile(), mode="wb")
GHB_F <- foreign::read.xport(tf)[,c("SEQN","LBXGH")]



#import bowel health data 2005-2010

#2005-2006
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2005-2006/BHQ_D.XPT", tf <- tempfile(), mode="wb")
BHQ_D <- foreign::read.xport(tf)[,c("SEQN","BHQ020","BHQ030","BHQ040")]

#2007-2008
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2007-2008/BHQ_E.XPT", tf <- tempfile(), mode="wb")
BHQ_E <- foreign::read.xport(tf)[,c("SEQN","BHQ020","BHQ030","BHQ040")]

#2009-2010
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2009-2010/BHQ_F.XPT", tf <- tempfile(), mode="wb")
BHQ_F <- foreign::read.xport(tf)[,c("SEQN","BHQ020","BHQ030","BHQ040")]


#import Questionaire (includes stroke)  data 2005-2010

#2005-2006
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2005-2006/MCQ_D.XPT", tf <- tempfile(), mode="wb")
MCQ_D <- foreign::read.xport(tf)[,c("SEQN","MCQ160F")]

#2007-2008
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2007-2008/MCQ_E.XPT", tf <- tempfile(), mode="wb")
MCQ_E <- foreign::read.xport(tf)[,c("SEQN","MCQ160F")]

#2009-2010
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2009-2010/MCQ_F.XPT", tf <- tempfile(), mode="wb")
MCQ_F <- foreign::read.xport(tf)[,c("SEQN","MCQ160F")]


#import BMI  data 2005-2010

#2005-2006
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2005-2006/BMX_D.XPT", tf <- tempfile(), mode="wb")
BMX_D <- foreign::read.xport(tf)[,c("SEQN","BMXBMI")]

#2007-2008
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2007-2008/BMX_E.XPT", tf <- tempfile(), mode="wb")
BMX_E <- foreign::read.xport(tf)[,c("SEQN","BMXBMI")]

#2009-2010
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2009-2010/BMX_F.XPT", tf <- tempfile(), mode="wb")
BMX_F <- foreign::read.xport(tf)[,c("SEQN","BMXBMI")]

#import tobacco  data 2005-2010

#2005-2006
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2005-2006/SMQRTU_D.XPT", tf <- tempfile(), mode="wb")
SMQRTU_D <- foreign::read.xport(tf)[,c("SEQN","SMQ680")]

#2007-2008
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2007-2008/SMQRTU_E.XPT", tf <- tempfile(), mode="wb")
SMQRTU_E <- foreign::read.xport(tf)[,c("SEQN","SMQ680")]

#2009-2010
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2009-2010/SMQRTU_F.XPT", tf <- tempfile(), mode="wb")
SMQRTU_F <- foreign::read.xport(tf)[,c("SEQN","SMQ680")]

#import hypertension data 2005-2010

#2005-2006
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2005-2006/BPQ_D.XPT", tf <- tempfile(), mode="wb")
BPQ_D <- foreign::read.xport(tf)[,c("SEQN","BPQ020")]

#2007-2008
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2007-2008/BPQ_E.XPT", tf <- tempfile(), mode="wb")
BPQ_E <- foreign::read.xport(tf)[,c("SEQN","BPQ020")]

#2009-2010
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2009-2010/BPQ_F.XPT", tf <- tempfile(), mode="wb")
BPQ_F <- foreign::read.xport(tf)[,c("SEQN","BPQ020")]

#import demographic data 2005-2010

#2005-2006
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2005-2006/DEMO_D.XPT", tf <- tempfile(), mode="wb")
DEMO_D <- foreign::read.xport(tf)[,c("SEQN", "RIDAGEYR", "RIAGENDR", "INDFMPIR", "DMDEDUC2", "RIDRETH1", "RIDEXPRG", "SDMVPSU", "SDMVSTRA", "SDDSRVYR", "WTMEC2YR")]

#2007-2008
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2007-2008/DEMO_E.XPT", tf <- tempfile(), mode="wb")
DEMO_E <- foreign::read.xport(tf)[,c("SEQN", "RIDAGEYR", "RIAGENDR", "INDFMPIR", "DMDEDUC2", "RIDRETH1", "RIDEXPRG", "SDMVPSU", "SDMVSTRA", "SDDSRVYR", "WTMEC2YR")]

#2009-2010
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2009-2010/DEMO_F.XPT", tf <- tempfile(), mode="wb")
DEMO_F <- foreign::read.xport(tf)[,c("SEQN", "RIDAGEYR", "RIAGENDR", "INDFMPIR", "DMDEDUC2", "RIDRETH1", "RIDEXPRG", "SDMVPSU", "SDMVSTRA", "SDDSRVYR", "WTMEC2YR")]

# import reproductive health data 2005-2010
#2005-2006
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2005-2006/RHQ_D.XPT", tf <- tempfile(), mode="wb")
RHQ_D <- foreign::read.xport(tf)[,c("SEQN","RHQ131","RHQ160")]

#2007-2008
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2007-2008/RHQ_E.XPT", tf <- tempfile(), mode="wb")
RHQ_E <- foreign::read.xport(tf)[,c("SEQN","RHQ131","RHQ160")]

#2009-2010
download.file("https://wwwn.cdc.gov/Nchs/Nhanes/2009-2010/RHQ_F.XPT", tf <- tempfile(), mode="wb")
RHQ_F <- foreign::read.xport(tf)[,c("SEQN","RHQ131","RHQ160")]



#combine diabetes dataset by rows *****NEED TO FIGURE OUT WHAT TO DO ABT 09-10********
DIQ <- bind_rows(DIQ_D, DIQ_E, DIQ_F)

#combine bowel health dataset by rows
BHQ <- bind_rows(BHQ_D, BHQ_E, BHQ_F)

#combine demographic data by rows
DEMO <- bind_rows(DEMO_D,DEMO_E,DEMO_F)

#combine glycohemoglobin data by rows
GHB <- bind_rows(GHB_D,GHB_E,GHB_F)

#combine stroke data by rows
MCQ <- bind_rows(MCQ_D,MCQ_E,MCQ_F)

#combine BMI data by rows
BMX <- bind_rows(BMX_D,BMX_E,BMX_F)

#combine tobacco data by rows
SMQRTU <- bind_rows(SMQRTU_D,SMQRTU_E,SMQRTU_F)

#combine hypertension data by rows
BPQ <- bind_rows(BPQ_D,BPQ_E,BPQ_F)

# combine parity by rows
RHQ <- bind_rows(RHQ_D,RHQ_E,RHQ_F)
# Try merging without exclude: DEMO & BHQ
DEMO_BHQ_merged<-merge(DEMO, BHQ, by='SEQN', all.x=TRUE)

# Merge ^ with GHB
DEMO_BHQ_GHB_merged<-merge(DEMO_BHQ_merged, GHB, by='SEQN', all.x=TRUE)

# Merge ^ with DIQ
DEMO_BHQ_GHB_DIQ_merged<-merge(DEMO_BHQ_GHB_merged, DIQ, by='SEQN', all.x=TRUE)

# Merge ^ with MCQ
DEMO_BHQ_GHB_DIQ_MCQ_merged<-merge(DEMO_BHQ_GHB_DIQ_merged, MCQ, by='SEQN', all.x=TRUE)

# Merge ^ with BMX
DEMO_BHQ_GHB_DIQ_MCQ_BMX_merged<-merge(DEMO_BHQ_GHB_DIQ_MCQ_merged, BMX, by='SEQN', all.x=TRUE)

# Merge ^ with SMQRTU (tobacco)
DEMO_BHQ_GHB_DIQ_MCQ_BMX_smok_merged<-merge(DEMO_BHQ_GHB_DIQ_MCQ_BMX_merged, SMQRTU, by='SEQN', all.x=TRUE)

# Merge ^ with BPQ (hypertension) calling it HTN
DEMO_BHQ_GHB_DIQ_MCQ_BMX_smok_HTN_merged<-merge(DEMO_BHQ_GHB_DIQ_MCQ_BMX_smok_merged, BPQ, by='SEQN', all.x=TRUE)

# Merge ^ with RHQ (parity) 
#DEMO_BHQ_GHB_DIQ_MCQ_BMX_smok_HTN_RHQ_merged
all_merged<-merge(DEMO_BHQ_GHB_DIQ_MCQ_BMX_smok_HTN_merged, RHQ, by='SEQN', all.x=TRUE)

NHANES_df<-all_merged
# CREATE NHANES SUBSET SURVEY


# create ANY_FI variable

for(x in 1:nrow(NHANES_df)) {

if(is.na(NHANES_df$BHQ020[x])  & is.na(NHANES_df$BHQ030[x])  & is.na(NHANES_df$BHQ040[x])){
  NHANES_df$ANY_FI[x]<- NA
} else if(NHANES_df$BHQ020[x] <=5 | NHANES_df$BHQ030[x] <=5  | NHANES_df$BHQ040[x]<=5 ) {
  NHANES_df$ANY_FI[x]<-1
} else {
  NHANES_df$ANY_FI[x]<-0
} 
}






# create new parity variable including never been pregnant

for(x in 1:nrow(NHANES_df)) {

if(is.na(NHANES_df$RHQ160[x]) & is.na(NHANES_df$RHQ131[x])) {
  NHANES_df$parity[x]<- NA
} 
  else if(is.na(NHANES_df$RHQ131[x])) {
  NHANES_df$parity[x]<-NA
} 
  else if (NHANES_df$RHQ131[x]==1) {
  NHANES_df$parity[x]<-NHANES_df$RHQ160[x]
} else if(NHANES_df$RHQ131[x] ==2) {
  NHANES_df$parity[x]<-0
}  else {
  NHANES_df$parity[x]<- NA
}
  
}



# creating categories for parity variable:

NHANES_df$parity<-cut(NHANES_df$parity, 
                    breaks=c(-Inf,0,1,2,Inf),labels=c("0", "1","2",">=3"))


# example code to turn hypertension to binary 0,1
NHANES_df$htn<-NHANES_df$BPQ020

NHANES_df$htn<-dplyr::recode(NHANES_df$htn, `1` = 1L, `2` = 0L)



#  code to turn stroke to binary 0,1
NHANES_df$stroke<-NHANES_df$MCQ160F

NHANES_df$stroke<-dplyr::recode(NHANES_df$stroke, `1` = 1L, `2` = 0L)


#  code to turn smoking to binary 0,1
NHANES_df$smoking<-NHANES_df$SMQ680

NHANES_df$smoking<-dplyr::recode(NHANES_df$smoking, `1` = 1L, `2` = 0L)



# creating factor var for gender
NHANES_df$gender<- NHANES_df$RIAGENDR
NHANES_df$gender<- factor(NHANES_df$RIAGENDR, labels=c("Men", "Women"))






# creating white & non-white categories for race variable:
for(x in 1:nrow(NHANES_df)) {

if(NHANES_df$RIDRETH1[x]==3) {
  NHANES_df$race[x]<- "white"
} else {
  NHANES_df$race[x]<- "non-white"
} 
}

NHANES_df$race <- 
  factor(NHANES_df$race, 
         levels=c("white","non-white"),
         labels=c("white", # Reference
                  "non-white"), exclude=NULL)




#create diabetes binary variable



for(x in 1:nrow(NHANES_df)) {
  if(is.na(NHANES_df$DIQ010[x]) & is.na(NHANES_df$DIQ050[x])) {
     NHANES_df$diabetes[x]<-NA
  }
  else if(NHANES_df$DIQ010[x] ==1 | NHANES_df$DIQ050[x] ==1) {
  NHANES_df$diabetes[x]<- 1
} else if(NHANES_df$DIQ010[x] ==2 | NHANES_df$DIQ010[x] ==3) {
  NHANES_df$diabetes[x]<-0
} else {
  NHANES_df$diabetes[x]<-NA
}
}
 


# create severe FI variable OK
for(x in 1:nrow(NHANES_df)) {

if(is.na(NHANES_df$BHQ020[x])  & is.na(NHANES_df$BHQ030[x])  & is.na(NHANES_df$BHQ040[x])) {
  NHANES_df$severe_FI[x]<- NA
} else if(NHANES_df$BHQ020[x] <=4 | NHANES_df$BHQ030[x] <=4  | NHANES_df$BHQ040[x]<=4 ) {
  NHANES_df$severe_FI[x]<-1
} else {
  NHANES_df$severe_FI[x]<-0
} 
}

# create a1c controlled/uncontrolled variable 


for(x in 1:nrow(NHANES_df)) {

if(is.na(NHANES_df$LBXGH[x])) {
  NHANES_df$uncontrolled_dm[x]<- NA
} else if(NHANES_df$LBXGH[x] <=7) {
  NHANES_df$uncontrolled_dm[x]<-0
} else {
  NHANES_df$uncontrolled_dm[x]<-1
} 
}

# risk of liquid stool
for(x in 1:nrow(NHANES_df)) {

if(is.na(NHANES_df$BHQ030[x])) {
  NHANES_df$liquid_stool[x]<- NA
} else if(NHANES_df$BHQ030[x] <=5) {
  NHANES_df$liquid_stool[x]<-1
} else {
  NHANES_df$liquid_stool[x]<-0
} 
}


# risk of solid stool
for(x in 1:nrow(NHANES_df)) {

if(is.na(NHANES_df$BHQ040[x])) {
  NHANES_df$solid_stool[x]<- NA
} else if(NHANES_df$BHQ040[x] <=5) {
  NHANES_df$solid_stool[x]<-1
} else {
  NHANES_df$solid_stool[x]<-0
} 
}


#fix sample weighting

NHANES_df<- mutate(NHANES_df, WTMEC6YR = WTMEC2YR/3)


NHANES_design<- svydesign(data= NHANES_df, strata = ~SDMVSTRA, id = ~ SDMVPSU, nest = TRUE, weights = ~WTMEC6YR)



#### FINISH CREATING SUBSET
# create subset



# subset 20+, nonpreg, only ppl with FI data
NHANES_subset <- subset(NHANES_design, ( RIDAGEYR>=20 & (RIDEXPRG!=1 | is.na(RIDEXPRG))  & !is.na(LBXGH) & !is.na(ANY_FI)))

# subset 20+, nonpreg, a1c, people w/ FI
NHANES_subset1 <- subset(NHANES_design, ( RIDAGEYR>=20 & (RIDEXPRG!=1 | is.na(RIDEXPRG)) & !is.na(LBXGH) & ANY_FI==1))
# CREATE TABLE 1 ,  using NHANES_subset (ppl with BHQ data (now with anyFI column added to NHANES_df))


mean_age<- svymean(~RIDAGEYR, NHANES_subset, na = TRUE)

mean_a1c<- svymean(~LBXGH, NHANES_subset, na = TRUE)

mean_BMI<- svymean(~BMXBMI, NHANES_subset, na = TRUE)

female_prop<-svyciprop(~I(RIAGENDR==2), NHANES_subset, method="likelihood")

#FI_prop<- svyciprop(~I(==2), NHANES_subset, method="likelihood")

stroke_prop<-svyciprop(~I(MCQ160F==1), NHANES_subset, method="likelihood")

hypertension_prop<-svyciprop(~I(BPQ020==1), NHANES_subset, method="likelihood")
print(hypertension_prop)

smoking_prop<-svyciprop(~I(SMQ680==1), NHANES_subset, method="likelihood")
#print(smoking_prop)

#poverty_prop<-svyciprop(~I(INDFMPIR<1), NHANES_subset, method="likelihood")
#print(smoking_prop)

mexican_prop<-svyciprop(~I(RIDRETH1==1), NHANES_subset, method="likelihood")
print(mexican_prop)

othhispanic_prop<-svyciprop(~I(RIDRETH1==2), NHANES_subset, method="likelihood")
print(othhispanic_prop)

white_prop<-svyciprop(~I(RIDRETH1==3), NHANES_subset, method="likelihood")
print(white_prop)

black_prop<-svyciprop(~I(RIDRETH1==4), NHANES_subset, method="likelihood")
print(black_prop)

otherrace_prop<-svyciprop(~I(RIDRETH1==5), NHANES_subset, method="likelihood")
print(otherrace_prop)
# CREATE TABLE 1 ,  using NHANES_subset1 (ppl with FI)



mean_age1<- svymean(~RIDAGEYR, NHANES_subset1, na = TRUE)

mean_a1c1<- svymean(~LBXGH, NHANES_subset1, na = TRUE)

mean_BMI1<- svymean(~BMXBMI, NHANES_subset1, na = TRUE)

female_prop1<-svyciprop(~I(RIAGENDR==2), NHANES_subset1, method="likelihood")

#FI_prop<- svyciprop(~I(==2), NHANES_subset1, method="likelihood")

stroke_prop1<-svyciprop(~I(MCQ160F==1), NHANES_subset1, method="likelihood")

hypertension_prop1<-svyciprop(~I(BPQ020==1), NHANES_subset1, method="likelihood")
print(hypertension_prop1)

smoking_prop1<-svyciprop(~I(smoking==1), NHANES_subset1, method="likelihood")
print(smoking_prop1)

poverty_prop1<-svyciprop(~I(INDFMPIR<1), NHANES_subset1, method="likelihood")
print(poverty_prop1)

parity0_prop1<-svyciprop(~I(parity==0), NHANES_subset1, method="likelihood")
print(parity0_prop1)

parity1_prop1<-svyciprop(~I(parity==1), NHANES_subset1, method="likelihood")
print(parity1_prop1)

parity2_prop1<-svyciprop(~I(parity==2), NHANES_subset1, method="likelihood")
print(parity2_prop1)


parity_3plus_prop1<-svyciprop(~I(parity==">=3"), NHANES_subset1, method="likelihood")
print(parity_3plus_prop1)


mexican_prop1<-svyciprop(~I(RIDRETH1==1), NHANES_subset1, method="likelihood")
print(mexican_prop1)


othhispanic_prop1<-svyciprop(~I(RIDRETH1==2), NHANES_subset1, method="likelihood")
print(othhispanic_prop1)

white_prop1<-svyciprop(~I(RIDRETH1==3), NHANES_subset1, method="likelihood")
print(white_prop1)

black_prop1<-svyciprop(~I(RIDRETH1==4), NHANES_subset1, method="likelihood")
print(black_prop1)

otherrace_prop1<-svyciprop(~I(RIDRETH1==5), NHANES_subset1, method="likelihood")
print(otherrace_prop1)
# CREATE TABLE 1 ,  using NHANES_subset2 (ppl without FI)

NHANES_subset2 <- subset(NHANES_design, ( RIDAGEYR>=20 & (RIDEXPRG!=1 | is.na(RIDEXPRG))  & !is.na(LBXGH) & ANY_FI==0))

mean_age2<- svymean(~RIDAGEYR, NHANES_subset2, na = TRUE)

mean_a1c2<- svymean(~LBXGH, NHANES_subset2, na = TRUE)

mean_BMI2<- svymean(~BMXBMI, NHANES_subset2, na = TRUE)

female_prop2<-svyciprop(~I(RIAGENDR==2), NHANES_subset2, method="likelihood")

#FI_prop<- svyciprop(~I(==2), NHANES_subset2, method="likelihood")

stroke_prop2<-svyciprop(~I(MCQ160F==1), NHANES_subset2, method="likelihood")
print(stroke_prop2)


hypertension_prop2<-svyciprop(~I(BPQ020==1), NHANES_subset2, method="likelihood")
print(hypertension_prop2)

smoking_prop2<-svyciprop(~I(SMQ680==1), NHANES_subset2, method="likelihood")
print(smoking_prop2)

poverty_prop2<-svyciprop(~I(INDFMPIR<1), NHANES_subset2, method="likelihood")
print(poverty_prop2)

parity0_prop2<-svyciprop(~I(parity==0), NHANES_subset2, method="likelihood")
print(parity0_prop2)

parity1_prop2<-svyciprop(~I(parity==1), NHANES_subset2, method="likelihood")
print(parity1_prop2)

parity2_prop2<-svyciprop(~I(parity==2), NHANES_subset2, method="likelihood")
print(parity2_prop2)


parity_3plus_prop2<-svyciprop(~I(parity==">=3"), NHANES_subset2, method="likelihood")
print(parity_3plus_prop2)

mexican_prop2<-svyciprop(~I(RIDRETH1==1), NHANES_subset2, method="likelihood")
print(mexican_prop2)

othhispanic_prop2<-svyciprop(~I(RIDRETH1==2), NHANES_subset2, method="likelihood")
print(othhispanic_prop2)

white_prop2<-svyciprop(~I(RIDRETH1==3), NHANES_subset2, method="likelihood")
print(white_prop2)

black_prop2<-svyciprop(~I(RIDRETH1==4), NHANES_subset2, method="likelihood")
print(black_prop2)

otherrace_prop2<-svyciprop(~I(RIDRETH1==5), NHANES_subset2, method="likelihood")
print(otherrace_prop2)


#getSummary(~Depression, ~Gender, NHANES)
# t - tests to get p val for characteristics of FI vs no FI groups

svyttest(RIDAGEYR~ANY_FI, NHANES_subset)

svyttest(LBXGH~ANY_FI, NHANES_subset)

svyttest(BMXBMI~ANY_FI, NHANES_subset)

svychisq(~MCQ160F + ANY_FI, NHANES_subset, statistic = "adjWald")

svychisq(~BPQ020 + ANY_FI, NHANES_subset, statistic = "adjWald")

svychisq(~SMQ680 + ANY_FI, NHANES_subset, statistic = "adjWald")

svychisq(~INDFMPIR+ ANY_FI, NHANES_subset, statistic = "adjWald")

svychisq(~RIDRETH1 + ANY_FI, NHANES_subset, statistic = "adjWald")
#LOGISTIC REGRESSION WITH NEWLY DEFINED CATEGORICAL VARIABLES
# RIAGENDR: 1 is male, 2 is female

# LOGISTIC REGRESSION: 
logit1 <- (svyglm(formula = ANY_FI~LBXGH+RIDAGEYR + gender + BMXBMI + stroke+ smoking+  htn + race , family=quasibinomial, design=NHANES_subset))
summary(logit1)
odds.ratio(logit1)

logit2<- (svyglm(formula = ANY_FI~LBXGH+RIDAGEYR + RIAGENDR + BMXBMI+MCQ160F + BPQ020 + INDFMPIR + SMQ680 + factor(RIDRETH1), family=quasibinomial, design=NHANES_subset))
summary(logit2)
exp(coef(logit2))


# 1) Severe FI vs non-severe FI (outcome), effect of a1c in people with FI defined by at least weekly episodes
sevFI_log1 <- (svyglm(formula = severe_FI~LBXGH+RIDAGEYR + gender + BMXBMI + stroke+ smoking+  htn + race , family=quasibinomial, design=NHANES_subset))
summary(sevFI_log1)
odds.ratio(sevFI_log1)


# 2) Effect of A1c in those with DM vs in those without DM
a1c_dm_log1<- (svyglm(formula = ANY_FI~LBXGH+RIDAGEYR + gender + BMXBMI + stroke+ smoking+  htn + race, family=quasibinomial, design=subset(NHANES_subset, diabetes==1)))
summary(a1c_dm_log1)
odds.ratio(a1c_dm_log1)

a1c_nodm_log1<- (svyglm(formula = ANY_FI~LBXGH+RIDAGEYR + gender + BMXBMI + stroke+ smoking+  htn + race, family=quasibinomial, design=subset(NHANES_subset, diabetes==0)))
summary(a1c_nodm_log1)
odds.ratio(a1c_nodm_log1)

# 3) Effect of A1c when measured as a binary variable (controlled vs uncontrolled, A1c<7 vs >=7)

binary_a1c_log1 <- (svyglm(formula = ANY_FI~uncontrolled_dm + RIDAGEYR + gender + BMXBMI + stroke+ smoking+  htn + race , family=quasibinomial, design=NHANES_subset))
summary(binary_a1c_log1)
odds.ratio(binary_a1c_log1)


# 4) Effect of A1c in white vs non-white fix
a1c_white_log1<- (svyglm(formula = ANY_FI~LBXGH + RIDAGEYR + gender + BMXBMI + stroke+ smoking+  htn, family=quasibinomial, design=subset(NHANES_subset, race== "white")))
summary(a1c_white_log1)
odds.ratio(a1c_white_log1)

a1c_nonwhite_log1<- (svyglm(formula = ANY_FI~LBXGH + RIDAGEYR + gender + BMXBMI + stroke+ smoking+  htn, family=quasibinomial, design=subset(NHANES_subset, race== "non-white")))
summary(a1c_nonwhite_log1)
odds.ratio(a1c_nonwhite_log1)

# effect of a1c in non-white when measured as a binary variable:
binarya1c_nonwhite_log1<- (svyglm(formula = ANY_FI~uncontrolled_dm + RIDAGEYR + gender + BMXBMI + stroke+ smoking+  htn, family=quasibinomial, design=subset(NHANES_subset, race== "non-white")))
summary(binarya1c_nonwhite_log1)
odds.ratio(binarya1c_nonwhite_log1)

# 5) effect of A1c in elderly (>= 65 yrs old)
a1c_elderly_log<- (svyglm(formula = ANY_FI~LBXGH+ gender + BMXBMI+ stroke + smoking + htn + race , family=quasibinomial, design=subset(NHANES_subset, RIDAGEYR>= 65)))
summary(a1c_elderly_log)
odds.ratio(a1c_elderly_log)

# effect of A1c in non-elderly (< 65 yrs old)
a1c_nonelderly_log<- (svyglm(formula = ANY_FI~LBXGH+ gender + BMXBMI+ stroke + smoking + htn + race , family=quasibinomial, design=subset(NHANES_subset, RIDAGEYR<65)))
summary(a1c_nonelderly_log)
odds.ratio(a1c_nonelderly_log)

# 6. Effect of A1c in overweight and obese (BMI >= 25)
a1c_obese_log<- (svyglm(formula = ANY_FI~LBXGH+ RIDAGEYR + gender +stroke + smoking + htn + race, family=quasibinomial, design=subset(NHANES_subset, BMXBMI>= 25)))
summary(a1c_obese_log)
odds.ratio(a1c_obese_log)

a1c_underweight_log<- (svyglm(formula = ANY_FI~LBXGH+ RIDAGEYR + gender +stroke + smoking + htn + race, family=quasibinomial, design=subset(NHANES_subset, BMXBMI< 18.5)))
summary(a1c_underweight_log)
odds.ratio(a1c_underweight_log)

# 7. Effect of A1c on liquid stool FI 
a1c_liquidFI_log<- (svyglm(formula = liquid_stool~LBXGH+ RIDAGEYR + gender + BMXBMI + stroke+ smoking+  htn + race, family=quasibinomial, design=NHANES_subset))
summary(a1c_liquidFI_log)
odds.ratio(a1c_liquidFI_log)

# 8. Effect of A1c on solid stool FI 
a1c_solidFI_log<- (svyglm(formula = solid_stool~LBXGH+ RIDAGEYR + gender + BMXBMI + stroke+ smoking+  htn + race, family=quasibinomial, design=NHANES_subset))
summary(a1c_solidFI_log)
odds.ratio(a1c_solidFI_log)

# 9. Effect of A1c in women 
a1c_women_log<- (svyglm(formula = ANY_FI~LBXGH+ RIDAGEYR  + BMXBMI + stroke+ smoking+  htn + race + parity, family=quasibinomial, design=subset(NHANES_subset, gender=="Women")))
summary(a1c_women_log)
odds.ratio(a1c_women_log)

# 10. Effect of A1c in men
a1c_men_log<- (svyglm(formula = ANY_FI~LBXGH+ RIDAGEYR  + BMXBMI + stroke+ smoking+  htn + race, family=quasibinomial, design=subset(NHANES_subset, gender=="Men")))
summary(a1c_men_log)
odds.ratio(a1c_men_log)