IDR es un paquete de funciones R para representar redes bayesianas y diagramas de influencia, analizar y evaluar los modelos. IDR permite: escribir la declaración del modelo: especificación de los nodos-variables y enlaces del DAG y las probabilidades y utilidades; añade items auxiliares a la estructura comprobar la corrección del modelo: ausencia de ciclos, repeticiones de nombres, distribuciones de probabilidad evaluar graficamente y/o numericamente la red: marginalizar las variables, calcular las alternativas óptimas instanciar nodos con valores del dominio y propagar la eviencia mostrar resumenes de los nodos: diagramas de probabilidad, matrices de adyacencias e influencias,…
Otras características de IDR son: exportar la red al formato de GeNie y al formato R la definición del modelo de probabilidad y utilidad puede ser: explícita mediante tablas o implícita mediante funciones o modelos (gml,…) la evaluación se puede realizar paso a paso: trazando las llamadas a cualquiera de las funciones volcando el estado de los nodos y del grafo generar redes aleatorias de tamaño, dominios y grado predefinido. exportar el resultado de la evaluación al formato KBM2L: tablas de probabilidad a posteriori y tablas de decisiones óptimas
El paquete está organizado en 4 modulos básicos: ( "idl-primitive.R") ("idl-potential2.R");( "idl-potential.R");( "idl-probability.R") ( "idl-network.R");( "idl-definition2.R");( "idl-definition.R") ("idl-operaciones.R");("idl-bayes.R");("idl-tables.R");("idl-instantiation.R"); ( "idl-evaluation2.R");( "idl-evaluation.R") Y otros modulos: queries, graficos, Utiliza los paquetes lattice, glm,…
Algunos detalles: las tablas de probabilidad son matrices bidimensionales las filas son distribuciones de probabilidad condicionadas (matrix - byrow) los valores de las variables condicionantes indexan las filas de arriba abajo con el orden derecha a izquierda la evaluación de las redes es mediante inversión de arcos. Puede ser sistematica, heurística o guiada, la evaluación de las redes es gráfica (solo se modifica el grafo y los tamaños de las tablas de probabilidad) y numérica (se realizan los calculos de las probabilidades y las utilidades. Variable global CALC <- TRUE/FALSE la matriz de adyacencia del grafo se construye con: mady[i,j] = 0, no hay influencia de i sobre j, mady[i,j] = 1, hay un arco desde i a j, condicional mady[i,j] = 2, hay un arco desde i a j, informativo, j es una decisión. R version 2.6.1 (2007-11-26) Copyright (C) 2007 The R Foundation for Statistical Computing ISBN 3-900051-07-0
Mejoras: leer el formato xdsl y generar código R otros algoritmos de evaluación (exactos y aproximados) dibujo automático de la red en GeNie tcp’s con glm e inferencia con el modelo algún algoritmo de aprendizaje varias medidas de relevancia/irrelevancia, algoritmo de simplificación de arcos algoritmo de análisis de sensibilidad generador de funciones de utilidad Implementar los escenarios de la consulta abierta ?
Ejemplo ASIA
Ejemplo ASIA RED rb = "asia" Nombre del modelo cat( "Bayesian network -- ", rb, " \n") mx.VisitToAsia <- matrix( data = c( 0.99, 0.01), nrow=1, ncol=2, byrow=TRUE, dimnames=NULL) node.VisitToAsia <- node(Type="CHANCE", Name="VisitToAsia", Values=c("NoVisit","Visit"), Preds=c(), Pots=mx.VisitToAsia) mx.Smoking <- matrix( data = c( 0.50, 0.50), node.Smoking <- node(Type="CHANCE", Name="Smoking", Values=c("NonSmoker","Smoker"), Pots=mx.Smoking) Nombre del modelo Variable aleatoria Tabla de probabilidad Marginal/Condicionada Nombre y Valores Predecesores o Padres
mx.Tuberculosis <- matrix( data = c( 0.99, 0.01, 0.95, 0.05), nrow = 2, ncol = 2, byrow = TRUE, dimnames = NULL) node.Tuberculosis <- node( Type = "CHANCE", Name = "Tuberculosis", Values=c("ABSENT","PRESENT"), Preds=c("VisitToAsia"), Pots=mx.Tuberculosis) mx.LungCancer <- matrix( data = c( 0.90, 0.10), node.LungCancer <- node( Type = "CHANCE", Name = "LungCancer", Values=c("ABSENT","PRESENT"), Preds=c("Smoking"), Pots=mx.LungCancer) mx.Bronchitis <- matrix( data = c( 0.70, 0.30, 0.40, 0.60), nrow = 2, ncol = 2, byrow = TRUE, dimnames = NULL) node.Bronchitis <- node( Type = "CHANCE", Name = "Bronchitis", Values=c("ABSENT","PRESENT"), Preds=c("Smoking"), Pots=mx.Bronchitis) mx.TbOrCa <- matrix( data = c( 1.0, 0.0, 0.0, 1.0, 0.0, 1.0), nrow = 4, ncol = 2, byrow = TRUE, dimnames = NULL) node.TbOrCa <- node( Type = "CHANCE", Name = "TbOrCa", Values=c("None","Sick"), Preds=c("Tuberculosis","LungCancer"), Pots=mx.TbOrCa)
mx.XRay <- matrix( data = c( 0.95, 0.05, 0.02, 0.98), nrow = 2, ncol = 2, byrow = TRUE, dimnames = NULL) node.XRay <- node( Type = "CHANCE", Name = "XRay", Values=c("NORMAL","ABNORMAL"), Preds=c("TbOrCa"), Pots=mx.XRay) mx.Dyspnea <- matrix( data = c( 0.90, 0.10, 0.20, 0.80, 0.30, 0.70, 0.10, 0.90), nrow = 4, ncol = 2, byrow = TRUE, dimnames = NULL) node.Dyspnea <- node( Type = "CHANCE", Name = "Dyspnea", Values=c("ABSENT","PRESENT"), Preds=c("TbOrCa","Bronchitis"), Pots=mx.Dyspnea)
diagrama = list( VisitToAsia = node.VisitToAsia, Smoking = node.Smoking, Tuberculosis = node.Tuberculosis, LungCancer = node.LungCancer, Bronchitis = node.Bronchitis, TbOrCa = node.TbOrCa, XRay = node.XRay, Dyspnea = node.Dyspnea ) asia <- bayesian.network.(diagrama) La red bayesiana es una lista de nodos Comprobaciones sintácticas y numericas ## idl-main, 03-06-08 rm(list=ls(all=TRUE)) #### DATA #### source("models/asia.R") #### PROC t1<-proc.time() ASIA <- marnod.eval( asia) t2<-proc.time() cat("TIME ", t2-t1, "\n") dump.netG( ASIA) dump.netR( ASIA) Inicialización del espacio de ejecución Carga del modelo Evaluación Volcado a formato xdsl y R IDR: INICIO SESION EJEMPLO
EVALUACIÓN Network summary: asia Network summary: asia 1] "VisitToAsia" [,1] [,2] [1,] 0.99 0.010 [1] "Smoking" [,1] [,2] [1,] 0.5 0.5 [1] "Tuberculosis" [1,] 0.989 0.01040 [1] "LungCancer" [1,] 0.945 0.0550 [1] "Bronchitis" [1,] 0.55 0.45 [1] "TbOrCa" [1,] 0.935 0.0648 [1] "XRay" [,1] [,2] [1,] 0.8897 0.110 [1] "Dyspnea" [1,] 0.564 0.435 EVALUACIÓN Network summary: asia Nodes: 8 Arcs: 8 Node: VisitToAsia gr: 0 COMPLEX 2 Preds: Node: Smoking Node: Tuberculosis gr: 1 COMPLEX 4 Preds: VisitToAsia Node: LungCancer gr: 1 COMPLEX 4 Preds: Smoking Node: Bronchitis Node: TbOrCa gr: 2 COMPLEX 8 Preds: Tuberculosis LungCancer Node: XRay gr: 1 COMPLEX 4 Preds: TbOrCa Node: Dyspnea gr: 2 COMPLEX 8 Preds: TbOrCa Bronchitis Network summary: asia Nodes: 8 Arcs: 12 Node: VisitToAsia gr: 1 COMPLEX 4 Preds: Tuberculosis Node: Smoking gr: 2 COMPLEX 8 Preds: LungCancer Bronchitis Node: Tuberculosis gr: 2 COMPLEX 8 Preds: LungCancer TbOrCa Node: LungCancer gr: 2 COMPLEX 8 Preds: Bronchitis TbOrCa Node: Bronchitis gr: 2 COMPLEX 8 Preds: TbOrCa Dyspnea Node: TbOrCa gr: 2 COMPLEX 8 Preds: XRay Dyspnea Node: XRay gr: 1 COMPLEX 4 Preds: Dyspnea Node: Dyspnea gr: 0 COMPLEX 2 Preds:
Ejemplo ASIA
RANDOM NETWORK 21 nodos, grado 3 máximo 29 arcos
Marginales RANDOM NETWORK 20 nodos, grado 3 máximo
EVALUACIÓN Network summary: random > print(Z[1][[1]]$mpot) Nodes: 21 Arcs: 29 Node: N1 gr: 0 COMPLEX 2 Preds: Node: N2 gr: 3 COMPLEX 16 Preds: N3 N13 N14 Node: N3 gr: 1 COMPLEX 4 Preds: N12 Node: N4 gr: 2 COMPLEX 8 Preds: N8 N21 Node: N5 gr: 1 COMPLEX 4 Preds: N17 Node: N6 Node: N7 gr: 1 COMPLEX 4 Preds: N18 Node: N8 Node: N9 gr: 1 COMPLEX 4 Preds: N4 Node: N10 gr: 3 COMPLEX 16 Preds: N2 N5 N6 Node: N11 gr: 2 COMPLEX 8 Preds: N13 N17 Node: N12 Node: N13 Node: N14 Node: N15 gr: 3 COMPLEX 16 Preds: N2 N4 N13 Node: N16 gr: 3 COMPLEX 16 Preds: N9 N10 N20 Node: N17 gr: 1 COMPLEX 4 Preds: N15 Node: N18 gr: 2 COMPLEX 8 Preds: N10 N14 Node: N19 gr: 1 COMPLEX 4 Preds: N16 Node: N20 gr: 2 COMPLEX 8 Preds: N4 N10 Node: N21 gr: 3 COMPLEX 16 Preds: N1 N6 N8 > print(Z[1][[1]]$mpot) [,1] [,2] [1,] 0.587 0.412 > print(Z[2][[1]]$mpot) [1,] 0.504 0.495 > print(Z[3][[1]]$mpot) [1,] 0.291 0.708 > print(Z[4][[1]]$mpot) [1,] 0.730 0.269 > print(Z[5][[1]]$mpot) [1,] 0.500 0.499 > print(Z[6][[1]]$mpot) [1,] 0.542 0.457 > print(Z[7][[1]]$mpot) [,1] [,2] [1,] 0.923 0.0761 ................... > print(Z[21][[1]]$mpot) [,1] [,2] [1,] 0.467 0.532 EVALUACIÓN
Condicionadas
Condicionadas
Condicionadas
Condicionadas
Condicionadas
Condicionadas
Condicionadas
Condicionadas
Condicionadas
Condicionadas
Condicionadas
RANDOM NETWORK 21 nodos, 168 arcos
Network summary: random Nodes: 21 Arcs: 168 Node: N1 gr: 4 COMPLEX 32 Preds: N6 N8 N4 N21 Node: N2 gr: 15 COMPLEX 65536 Preds: N3 N5 N6 N7 N8 N9 N10 N11 N12 N13 N14 N15 N16 N20 N21 Node: N3 gr: 14 COMPLEX 32768 Preds: N5 N6 N7 N8 N9 N10 N11 N12 N13 N14 N15 N16 N20 N21 Node: N4 gr: 12 COMPLEX 8192 Preds: N10 N2 N13 N5 N6 N8 N9 N11 N15 N16 N20 N21 Node: N5 gr: 14 COMPLEX 32768 Preds: N6 N7 N8 N9 N10 N11 N12 N13 N14 N15 N16 N17 N20 N21 Node: N6 gr: 13 COMPLEX 16384 Preds: N7 N8 N9 N10 N11 N12 N13 N14 N15 N16 N17 N20 N21 Node: N7 gr: 13 COMPLEX 16384 Preds: N8 N9 N10 N11 N12 N13 N14 N15 N16 N17 N18 N20 N21 Node: N8 gr: 12 COMPLEX 8192 Preds: N9 N10 N11 N12 N13 N14 N15 N16 N17 N18 N20 N21 Node: N9 gr: 11 COMPLEX 4096 Preds: N10 N11 N12 N13 N14 N15 N16 N17 N18 N20 N21 Node: N10 gr: 10 COMPLEX 2048 Preds: N11 N12 N13 N14 N15 N16 N17 N18 N20 N21 Node: N11 gr: 9 COMPLEX 1024 Preds: N12 N13 N14 N15 N16 N17 N18 N20 N21 Node: N12 gr: 8 COMPLEX 512 Preds: N13 N14 N15 N16 N17 N18 N20 N21 Node: N13 gr: 7 COMPLEX 256 Preds: N14 N15 N16 N17 N18 N20 N21 Node: N14 gr: 6 COMPLEX 128 Preds: N15 N16 N17 N18 N20 N21 Node: N15 gr: 5 COMPLEX 64 Preds: N16 N17 N18 N20 N21 Node: N16 gr: 5 COMPLEX 64 Preds: N17 N18 N19 N20 N21 Node: N17 gr: 4 COMPLEX 32 Preds: N18 N19 N20 N21 Node: N18 gr: 3 COMPLEX 16 Preds: N19 N20 N21 Node: N19 gr: 2 COMPLEX 8 Preds: N20 N21 Node: N20 gr: 1 COMPLEX 4 Preds: N21 Node: N21 gr: 0 COMPLEX 2 Preds: