-
Notifications
You must be signed in to change notification settings - Fork 0
/
R_code_bn
101 lines (88 loc) · 2 KB
/
R_code_bn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#Install dagitty
install.packages("devtools") # unless you have it already
library(devtools)
devtools::install_github("jtextor/dagitty/r")
#install_github("jtextor/dagitty/r")
#Optie unlooped
library( dagitty )
g <- dagitty('
dag {
Absences -> Failures
Absences -> Grades
Activities -> Freetime
Age -> Activities
Age -> Romantic
Age -> Studytime
Failures -> Grades
Failures -> Paid
Failures -> Schoolsup
Failures -> Studytime
Famrel -> Famsup
Famrel -> Paid
Famsup -> Grades
Famsup -> Paid
Famsup -> Studytime
Fedu -> Fjob
Fedu -> Higher
Fjob -> Famrel
Freetime -> Grades
Freetime -> Studytime
Goout -> Freetime
Goout -> Romantic
Health -> Absences
Health -> Grades
Health -> Studytime
Higher -> Famsup
Higher -> School
Higher -> Studytime
Medu -> Higher
Medu -> Mjob
Mjob -> Famrel
Paid -> Freetime
Paid -> Grades
Paid -> Studytime
Romantic -> Freetime
School -> Schoolsup
School -> Traveltime
Schoolsup -> Freetime
Schoolsup -> Grades
Schoolsup -> Studytime
Studytime -> Grades
Subject -> Absences
Subject -> Failures
Subject -> Famsup
Subject -> Grades
Subject -> Paid
Subject -> Schoolsup
Subject -> Studytime
Traveltime -> Absences
Traveltime -> Freetime
}
')
# Print out all d-separation statements implied by the DAG.
impliedConditionalIndependencies(g)
# This single command performs a chi-square test for all implied conditional
# independencies from g on the dataset g, and reports the statistic, p-value
# and the RMSEA for each test.
localTests(g,d,type="cis.chisq")
install.packages('bnlearn’)
library(bnlearn)
#Import data
d <- read.csv("/home/mdegrauw/Downloads/bn-master/matpor_concat.csv")
#Remove duplicate rownumber row
d <- d[,-1]
# Let's take a quick look at the levels and distributions of each variable.
for( i in colnames(d) ){
print(i)
print(table(d[,i]))
}
g2 <- pc.stable(d)
plot(g2)
amat(g2)
g3 <- pc.stable(diabetes,alpha=0.01)
plot(g3)
amat(g3)
d$G1 <- cut(d$G1, c(-1,5,10,15,20))
d$G2 <- cut(d$G2, c(-1,5,10,15,20))
d$G3 <- cut(d$G3, c(-1,5,10,15,20))
d$absences <- cut(d$absences, c(-1,0,5,10,20,75))