Learn R Programming

SemiSupervised (version 1.0)

predict.agraph: Out-of-Sample Predict Procedure for agraph

Description

This implements the out-of-sample prediction for an ‘agraph’ object.

Usage

# S4 method for agraph
predict(object,xnew,gnew,type=c("vector","response","prob"),…)

Arguments

object

an existing ‘agraph’ object.

xnew

an object of class ‘data.frame’, ‘vector’, or ‘matrix’. This is not always necessary and depends on call context (refer to details below).

gnew

the ‘matrix’ of new graph links between the data to predict and the data used for training. This is not always necessary and depends on call context (refer to details below).

type

the type of prediction to return.

mop up additional arguments.

Value

If type(object) is ‘r’, a vector of predicted values is returned. If type(object) is ‘c’, the object returned depends on the type argument.

Details

The prediction inputs are dependent upon how one calls the original agraph generic function. The cases are discussed next:

1) y~.: This is the default and most common case. Set xnew to your new hold-out data set and do not initialize gnew.

2) y~aG(g): Prior to applying the predict, we must first update the ‘anchor’ object used in the original agraph call to the xnew data. To do this, invoke AnchorGraph call with fit.g set to the original ‘anchor’ object. Fit the updated Anchor Graph as the gnew parameter and ignore the xnew parameter.

3) y~. + aG(g): Prior to applying the predict, we must first update the ‘anchor’ object used in the original agraph call to some new data set that correponds to rows of xnew (it techically doesn't have to be xnew'). Proceed as in case (2) except the xnew data must be provided.

4) Non-formula call: xnew will either be provided or NULL depending on context, and gnew will have to be provided as in case two.

Examples

Run this code
# NOT RUN {
## Prediction depends on the nature of the call. Consider some examples.
library(mlbench)
data(Sonar)

n=dim(Sonar)[1]
p=dim(Sonar)[2]

nu=0.2
set.seed(100)
L=sort(sample(1:n,ceiling(nu*n)))
U=setdiff(1:n,L)
U1=sample(U,ceiling(0.5*n))

y.true<-Sonar$Class
Sonar$Class[U]=NA

## Typical, call to agraph and predict

g.agraph1<-agraph(Class~.,data=Sonar[c(L,U1),])
p.agraph1<-predict(g.agraph1,xnew=Sonar[U,-p])
tab=table(y.true[U],p.agraph1)
1-sum(diag(tab))/sum(tab)
# }
# NOT RUN {
## Predict the graph only case
ctrl=SemiSupervised.control()
scale.x=x.scaleL(Sonar[,-p],L)
g=AnchorGraph(scale.x[c(L,U1),-p],control=ctrl)
g.agraph2<-agraph(Class~aG(g),data=Sonar[c(L,U1),],control=ctrl)
g<-AnchorGraph(scale.x[U,-p],fit.g=g,control=ctrl)
p.agraph2<-predict(g.agraph2,gnew=g)
tab=table(y.true[U],p.agraph2)
1-sum(diag(tab))/sum(tab)
# }

Run the code above in your browser using DataLab