# Load data and package
data1<-read.csv("D:/Dropbox/Research/@_Team/@@_AISDL_Fandom/BMF collaborative projects/2025/Datasets/Nigeria Electricity/peoplesun_ent_anon.csv",header = TRUE,stringsAsFactors = TRUE)
library(bayesvl)


data1$Gender<-data1$q213_1
data1$FormalFinancialAccess<-data1$q214
data1$InformalFinancialAccess<-data1$q215
data1$FriendsFamilyRelatives<-data1$q216__11
data1$ElecReliability<-data1$q604_1
data1$ElecAffordability<-data1$q604_2
data1$ElecSufficiency<-data1$q604_3


keeps <- c("ElecReliability","ElecAffordability","ElecSufficiency","Gender",
           "FormalFinancialAccess","InformalFinancialAccess","FriendsFamilyRelatives")
data1 <- data1[keeps]
data1<-na.omit(data1) 

# Model construction 1 with prior reflecting belief
model1a<-bayesvl()
model1a<-bvl_addNode(model1a,"ElecSufficiency","norm")
model1a<-bvl_addNode(model1a,"Gender","binom")
model1a<-bvl_addNode(model1a,"FormalFinancialAccess","binom")
model1a<-bvl_addNode(model1a,"InformalFinancialAccess","binom")
model1a<-bvl_addNode(model1a,"FriendsFamilyRelatives","binom")

model1a<-bvl_addNode(model1a,"Formal_Gender","trans")
model1a<-bvl_addArc(model1a,"Gender","Formal_Gender","*")
model1a<-bvl_addArc(model1a,"FormalFinancialAccess","Formal_Gender","*")

model1a<-bvl_addNode(model1a,"Informal_Gender","trans")
model1a<-bvl_addArc(model1a,"Gender","Informal_Gender","*")
model1a<-bvl_addArc(model1a,"InformalFinancialAccess","Informal_Gender","*")

model1a<-bvl_addNode(model1a,"FFR_Gender","trans")
model1a<-bvl_addArc(model1a,"Gender","FFR_Gender","*")
model1a<-bvl_addArc(model1a,"FriendsFamilyRelatives","FFR_Gender","*")

model1a<-bvl_addArc(model1a,"FormalFinancialAccess","ElecSufficiency","slope")
model1a<-bvl_addArc(model1a,"Formal_Gender","ElecSufficiency","slope")
model1a<-bvl_addArc(model1a,"InformalFinancialAccess","ElecSufficiency","slope")
model1a<-bvl_addArc(model1a,"Informal_Gender","ElecSufficiency","slope")
model1a<-bvl_addArc(model1a,"FriendsFamilyRelatives","ElecSufficiency","slope")
model1a<-bvl_addArc(model1a,"FFR_Gender","ElecSufficiency","slope")

bvl_bnPlot(model1a)

# Generate Stan code
model_string1a<- bvl_model2Stan(model1a)
cat(model_string1a) 

# Model Fit
model1a<-bvl_modelFit(model1a, data1, warmup = 2000, iter = 5000, chains = 1,cores = 4) 
summary(model1a)



# Model construction 2 with prior reflecting belief
model2a<-bayesvl()
model2a<-bvl_addNode(model2a,"ElecAffordability","norm")
model2a<-bvl_addNode(model2a,"Gender","binom")
model2a<-bvl_addNode(model2a,"FormalFinancialAccess","binom")
model2a<-bvl_addNode(model2a,"InformalFinancialAccess","binom")
model2a<-bvl_addNode(model2a,"FriendsFamilyRelatives","binom")

model2a<-bvl_addNode(model2a,"Formal_Gender","trans")
model2a<-bvl_addArc(model2a,"Gender","Formal_Gender","*")
model2a<-bvl_addArc(model2a,"FormalFinancialAccess","Formal_Gender","*")

model2a<-bvl_addNode(model2a,"Informal_Gender","trans")
model2a<-bvl_addArc(model2a,"Gender","Informal_Gender","*")
model2a<-bvl_addArc(model2a,"InformalFinancialAccess","Informal_Gender","*")

model2a<-bvl_addNode(model2a,"FFR_Gender","trans")
model2a<-bvl_addArc(model2a,"Gender","FFR_Gender","*")
model2a<-bvl_addArc(model2a,"FriendsFamilyRelatives","FFR_Gender","*")

model2a<-bvl_addArc(model2a,"FormalFinancialAccess","ElecAffordability","slope")
model2a<-bvl_addArc(model2a,"Formal_Gender","ElecAffordability","slope")
model2a<-bvl_addArc(model2a,"InformalFinancialAccess","ElecAffordability","slope")
model2a<-bvl_addArc(model2a,"Informal_Gender","ElecAffordability","slope")
model2a<-bvl_addArc(model2a,"FriendsFamilyRelatives","ElecAffordability","slope")
model2a<-bvl_addArc(model2a,"FFR_Gender","ElecAffordability","slope")

bvl_bnPlot(model2a)

# Generate Stan code
model_string1a<- bvl_model2Stan(model2a)
cat(model_string1a) 

# Model Fit
model2a<-bvl_modelFit(model2a, data1, warmup = 2000, iter = 5000, chains = 1,cores = 4) 
summary(model2a)



# Model construction 3 with prior reflecting belief
model3a<-bayesvl()
model3a<-bvl_addNode(model3a,"ElecReliability","norm")
model3a<-bvl_addNode(model3a,"Gender","binom")
model3a<-bvl_addNode(model3a,"FormalFinancialAccess","binom")
model3a<-bvl_addNode(model3a,"InformalFinancialAccess","binom")
model3a<-bvl_addNode(model3a,"FriendsFamilyRelatives","binom")

model3a<-bvl_addNode(model3a,"Formal_Gender","trans")
model3a<-bvl_addArc(model3a,"Gender","Formal_Gender","*")
model3a<-bvl_addArc(model3a,"FormalFinancialAccess","Formal_Gender","*")

model3a<-bvl_addNode(model3a,"Informal_Gender","trans")
model3a<-bvl_addArc(model3a,"Gender","Informal_Gender","*")
model3a<-bvl_addArc(model3a,"InformalFinancialAccess","Informal_Gender","*")

model3a<-bvl_addNode(model3a,"FFR_Gender","trans")
model3a<-bvl_addArc(model3a,"Gender","FFR_Gender","*")
model3a<-bvl_addArc(model3a,"FriendsFamilyRelatives","FFR_Gender","*")

model3a<-bvl_addArc(model3a,"FormalFinancialAccess","ElecReliability","slope")
model3a<-bvl_addArc(model3a,"Formal_Gender","ElecReliability","slope")
model3a<-bvl_addArc(model3a,"InformalFinancialAccess","ElecReliability","slope")
model3a<-bvl_addArc(model3a,"Informal_Gender","ElecReliability","slope")
model3a<-bvl_addArc(model3a,"FriendsFamilyRelatives","ElecReliability","slope")
model3a<-bvl_addArc(model3a,"FFR_Gender","ElecReliability","slope")

bvl_bnPlot(model3a)

# Generate Stan code
model_string1a<- bvl_model2Stan(model3a)
cat(model_string1a) 

# Model Fit
model3a<-bvl_modelFit(model3a, data1, warmup = 2000, iter = 5000, chains = 1,cores = 4) 
summary(model3a)