Learn R Programming

CRF (version 0.3-8)

make.crf: Make CRF

Description

Generate CRF from the adjacent matrix

Usage

make.crf(adj.matrix = NULL, n.states = 2, n.nodes = 2)

Arguments

adj.matrix
The adjacent matrix of CRF network.
n.states
The state numbers of nodes.
n.nodes
The number of nodes, which is only used to generate linear chain CRF when adj.matrix is NULL.

Value

  • The function will return a new CRF, which is an environment with components:
  • n.nodesThe number of nodes.
  • n.edgesThe number of edges.
  • n.statesThe number of states for each node. It is a vector of length n.nodes.
  • max.stateThe maximum number of states. It is equal to max(n.states).
  • edgesThe node pair of each edge. It is a matrix with 2 columns and n.edges rows. Each row denotes one edge. The node with smaller id is put in the first column.
  • n.adjThe number of adjacent nodes for each node. It is a vector of length n.nodes.
  • adj.nodesThe list of adjacent nodes for each node. It is a list of length n.nodes and the i-th element is a vector of length n.adj[i].
  • adj.edgesThe list of adjacent edges for each node. It is similiar to adj.nodes while contains the edge ids instead of node ids.
  • node.potThe node potentials. It is a matrix with dimmension (n.nodes, max.state). Each row node.pot[i,] denotes the node potentials of the i-th node.
  • edge.potThe edge potentials. It is a list of n.edges matrixes. Each matrix edge.pot[[i]], with dimension (n.states[edges[i,1]], n.states[edges[i,2]]), denotes the edge potentials of the i-th edge.

Details

The function will generate an empty CRF from a given adjacent matrix. If the length of nstates is less than n.nodes, it will be used repeatly. All node and edge potentials are initilized as 1.

Since the CRF data are often very huge, CRF is implemented as an environment. The assignment of environments will only copy the addresses instead of real data, therefore the variables using normal assignment will refer to the exactly same CRF. For complete duplication of the data, please use duplicate.crf.

See Also

duplicate.crf, clamp.crf, sub.crf

Examples

Run this code
library(CRF)

nNodes <- 4
nStates <- 2

adj <- matrix(0, nrow=nNodes, ncol=nNodes)
for (i in 1:(nNodes-1))
{
	adj[i,i+1] <- 1
	adj[i+1,i] <- 1
}

crf <- make.crf(adj, nStates)

crf$node.pot[1,] <- c(1, 3)
crf$node.pot[2,] <- c(9, 1)
crf$node.pot[3,] <- c(1, 3)
crf$node.pot[4,] <- c(9, 1)

for (i in 1:crf$n.edges)
{
   crf$edge.pot[[i]][1,] <- c(2, 1)
   crf$edge.pot[[i]][2,] <- c(1, 2)
}

Run the code above in your browser using DataLab