Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to make a bayesian network in R . I am stating summary of my data and code in R . but the

I am trying to make a bayesian network in R. I am stating summary of my data and code in R. but the result of my Bayesian network is seems weird and incorrect.
Please send me corrected code here.
summary(data)
Age Band of Driver Sex of Driver Educational Level Driving Experience
18-30 :4209 Female : 696 Above high school : 3575-10yr :3322
31-50 :4033 Male :11285 Elementary school :21392-5yr :2567
Over 51 :1561 Unknown: 177 High school :1095 Above 10yr:2233
Under 18: 813 Illiterate : 451-2yr :1735
Unknown :1542 Junior high school:7522 Below 1yr :1335
Unknown : 827 Unknown : 816
Writing & reading : 173(Other) : 150
Types of Junction Road Surface Type Light Conditions
Y Shape :4490 Asphalt roads :11148 Darkness - lights lit :3220
No junction:3766 Asphalt roads with some distress: 80 Darkness - lights unlit: 40
Crossing :2165 Earth roads : 351 Darkness - no lighting : 187
Unknown :1059 Gravel roads : 242 Daylight :8711
Other : 443 Other : 166
O Shape : 163 Unknown : 171
(Other) : 72
Weather Conditions Type of Collision Vehicle Movement
Normal :9928 Vehicle with vehicle collision :8670 Going straight :8059
Raining:1308 Collision with roadside objects:1762 Other :1316
Other : 296 Collision with pedestrians : 874 Moving Backward: 973
Unknown: 292 Rollover : 393 Reversing : 556
Cloudy : 125 Collision with animals : 169 Turnover : 482
Windy : 98 Unknown : 167 Getting off : 334
(Other): 111(Other) : 123(Other) : 438
Cause of Accident Accident Severity
ChangeLane :3242 Light : 1743
NoDistance :2243 Serious:10415
NoPriority :1910
DriveHighOrCareless:1552
Other :1458
MovingBackward :1111
(Other) : 642
Code: library(readr)
library(bnlearn)
library(gRain)
library(ggplot2)
library(reshape2)
library(forcats)
library(dplyr)
data <- read.csv("F:/2nd Semester/Data Processing/Bayesian/Dataset/archive (2)/cleaned.csv")
data = lapply(data, as.factor)
data = data.frame(data)
levels_Movement = c("Enteringajunction", "Getting off", "Going straight",
"Moving Backward","Other","Overtaking","Parked","Reversing"
,"Stopping","Turnover","U-Turn","Unknown","Waiting to go")
levels(data$Vehicle_movement)= c("Entering a junction", "Getting off", "Going straight",
"Moving Backward", "Other", "Overtaking","Parked","Reversing"
,"Stopping","Turnover","U-Turn","Other","Waiting to go")
levels(data$Cause_of_accident)= c("ChangeLane", "ChangeLane", "DriveHighOrCareless",
"DriveHighOrCareless", "DriveLeft", "DriveAlcohol",
"DriveAlcohol", "Other", "Other","MovingBackward",
"NoDistance", "NoPriority","NoPriority", "Other","Other",
"Other","Other","Other","Other","Other")
# Convert the "Accident_severity" column to character
data$Accident_severity = as.character(data$Accident_severity)
# Convert the "Accident_severity" column to numeric
data$Accident_severity = as.numeric(data$Accident_severity)
# Now, use the cut() function
data$Accident_severity = cut(data$Accident_severity, breaks = c(0,1,2), labels = c("Light", "Serious"))
data = subset(data, select =-c(Vehicle_driver_relation,Lanes_or_Medians,Pedestrian_movement))
data <- na.omit(data)
data <- data %>% mutate_if(is.numeric, ~ifelse(is.na(.), mean(., na.rm = TRUE),.))
column_names= c("Age Band of Driver", "Sex of Driver", "Educational Level", "Driving Experience","Types of Junction",
"Road Surface Type", "Light Conditions", "Weather Conditions", "Type of Collision","Vehicle Movement", "Cause of Accident", "Accident Severity")
colnames(data )= column_names
summary(data)
tabu(data)
mmhc(data)
h2pc(data)
# Create a Bayesian network structure using the 'hill climbing' algorithm
network <- hc(data)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions

Question

55. Show that g(t) tet cannot be a moment generating function.

Answered: 1 week ago