# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .r
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: R
#     language: R
#     name: ir
# ---

# + [markdown] deletable=true editable=true
# # Part1: process input ACS data

# + deletable=true editable=true
bay_acs_2 <- read.csv(file="data/bay_acs_factored.csv",sep=",")

# + deletable=true editable=true
str(bay_acs_2)

# + deletable=true editable=true
colnames(bay_acs_2)[which(colnames(bay_acs_2) %in% c("X1","X2","X3") )] <- c("AGE","SEX","VEH")
colnames(bay_acs_2)[which(colnames(bay_acs_2) %in% c("X4","X5") )] <- c("JWTR","PINCP")
colnames(bay_acs_2)[which(colnames(bay_acs_2) %in% c("X6","X7","X8") )] <- c("AGE_S","SEX_S","VEH_S")
colnames(bay_acs_2)[which(colnames(bay_acs_2) %in% c("X9","X10") )] <- c("JWTR_S","PINCP_S")

# + deletable=true editable=true
bay_acs_2[,c("X")] <- list(NULL)
data <- lapply(bay_acs_2, factor)
data <-as.data.frame(data)
str(data)

# + [markdown] deletable=true editable=true
# # Part 2: bnlearn

# + deletable=true editable=true
# install.packages("bnlearn", repos = "http://cran.us.r-project.org")
library(bnlearn, quietly = T)

# + deletable=true editable=true
whitelist = data.frame(from = c("SEX"), to = c("PINCP_index"))
whitelist
bn.hc <- hc(data[complete.cases(data),],whitelist = whitelist)
#bn.hc <- hc(data,whitelist = whitelist)

# + deletable=true editable=true
plot(bn.hc)

# + [markdown] deletable=true editable=true
# # Part3:plot

# + deletable=true editable=true
#install.packages("hexbin", repos = "http://cran.us.r-project.org")
library(hexbin)
smoothScatter(data$AGE_S_index, data$PINCP_index,main='smooth scatter plot of PUMA',xlab='Age level of spouse',ylab='Income level of householder',cex.main=2, cex.lab=2,cex.axis=2)

# + deletable=true editable=true
smoothScatter(data$AGE_index, data$AGE_S_index,main='smooth scatter plot of PUMA',xlab='Age level of householder',ylab='Age level of spouse',cex.main=2, cex.lab=2,cex.axis=2)

# + [markdown] deletable=true editable=true
# # Part4:simulation

# + deletable=true editable=true
fit = bn.fit(bn.hc,data[complete.cases(data),] )

# + deletable=true editable=true
fit$SEX

# + deletable=true editable=true
methods(simulate)
sim = rbn(fit, n = 1000000, debug = FALSE)
str(sim)

# + deletable=true editable=true
# http://www.uni-kiel.de/psychologie/rexrepos/posts/diagDistributions.html
smoothScatter(sim$AGE_S_index, sim$PINCP_index,main = 'smooth plot of simulated data \nusing Bayesian Networks',xlab='Age level of spouse',ylab='Income level of householder',cex.main=2, cex.lab=2,cex.axis=2)

# + deletable=true editable=true
smoothScatter(sim$AGE_index, sim$AGE_S_index,main = 'smooth plot of simulated data \nusing Bayesian Networks',xlab='Age level of householder',ylab='Age level of spouse',cex.main=2, cex.lab=2,cex.axis=2)

# + deletable=true editable=true
write.csv(sim, file = "../output/bnlearn/bnlearn_run1.csv",row.names=TRUE)

# + deletable=true editable=true


StarcoderdataJupyterScriptsDedupFiltered