plot.mixreg {mixreg}R Documentation

Plot fitted mixtures of regressions.

Description

Plots the fitted regression lines for one or two mixtures of regression models as fitted by mixreg() (or possibly by visualFit()).

Usage

   ## S3 method for class 'mixreg'
plot(x, y, cMeth=c("none","distance","prob"),
                         ptcol="black",col=NULL,col2=NULL,lty=NULL,lty2=NULL,
                         add=FALSE, warn=TRUE, modnames=NULL, legPos = "topleft", ...)

Arguments

x

An object of class "mixreg" as returned by mixreg() or possibly by visualFit().

y

If supplied it must be an object of class "mixreg".

cMeth

Text string specifying the “classification method”. If cMeth is "none" no classification is done. If cMeth is "distance" then points are classified as “belonging” to a component of the mixture according to which of the regression lines they are nearest to, in terms of Euclidean distance. If cMeth is "prob" then points are classified as “belonging” to a component of the mixture according to which of the probabilities gamma[i,j] of their being associated with that component is largest. If the points are classified then they are plotted in colours corresponding to that classification.

ptcol

Scalar specfying the colour in which the data points are plotted. Ignored unless cMeth="none". (In other cases the data points are plotted in colours corresponding to their classification.)

col

Vector specifying the colours in which the fitted lines, corresponding to each of the components, should be plotted. Defaults to 1:K where K is the number of components. These are also the colours in which the data points corresponding to each of the components are plotted when cMeth differs from "none".

col2

Like col except that it specifies the colours used to plot the second model, specified by y. Ignored if y is missing. Not used for point colours.

lty

Vector specifying the line types used for plotting the fitted lines corresponding to each of the components. Defaults to 1 (i.e. all lines are solid).

lty2

Like lty except that it specifies the line types used to plot the second model, specified by y. Ignored if y is missing.

add

For internal use only.

warn

Logical scalar; should a warning be issued if the data for the plot being added (those corresponding to argument y) differ from those for the existing plot? Ignored if y is missing.

modnames

Character vector, of length 2, of “model names”. Used by legend() when adding the plot of the second model (specified by y) to the initial plot. Ignored if y is missing, and of course is also ignored if legPos is NULL. The first entry is the name of the model corresponding to x and the second is the name corresponding to y. Defaults to c("Model 1","Model 2").

legPos

A list with entries x and y, or a text string, specifying the placement of the legend. See legend() for details. The plotting of a legend may be suppressed by setting legPos=NULL. Only one legend is plotted even if two models are specified; which “pieces” correspond to which model is indicated by modnames.

...

Optional extra arguments for plot(), points() and lines(). These may include xlim, ylim, xlab, ylab, pch, and main. Note that ... is used only when plotting the model specified by x.

Value

If both x and y models are both specified, or if cMeth is "none", then no value is returned. Otherwise the value is the data component of the x argument of this function, augmented by an extra column groups. This column is a factor that specifies the component to which each point has been assigned. This data frame also has an attribute "cMeth", the value of the cMeth argument.

Details about superimposition of plots

Superimposing a plot of one fitted mixture of regressions model upon that of another such model really makes sense only if the same underlying data set is involved. The code checks whether the data sets appear to be the same, and if not, issues a warning when warn is TRUE.

Note that only the data points corresponding to the first model (i.e. that corresponding to argument x) are plotted. The resulting graphic would be too cluttered otherwise, and anyway, as was said initially, the plot doesn't really make sense if different data sets are involved.

Author(s)

Rolf Turner r.turner@auckland.ac.nz

See Also

plot.cband(), plot.mixresid(), qqMix(),

Examples

    thStrt <- list(
                   list(beta=c(26.07,48808),sigsq=1.1573,lambda=0.33333333),
                   list(beta=c(23.48,32387),sigsq=1.8730,lambda=0.33333333),
                   list(beta=c(-0.0597,20760),sigsq=0.2478,lambda=0.33333333)
                 )
    kfit  <- mixreg(y ~ x,ncomp=3,data=kilnAoneOut,thetaStart=thStrt)
    plot(kfit,pch=8,col=c("red","green","blue","black"))
    plot(kfit,pch=8,col=c("green","blue","black"),cMeth="d")
    plot(kfit,pch=8,col=c("green","blue","black"),cMeth="p")
    afit <- mixreg(plntsInf ~ aphRel,data=aphids,ncomp=2)
    plot(afit,cMeth="p",col=c("blue","red"),pch=20)
    # Separates the points into two groups incredibly clearly!
    npfit <- mixreg(plntsInf ~ 1, data=aphids,ncomp=2)
    plot(npfit,cMeth="p",col=c("blue","red"),pch=20)
    ## Not run: 
        vfit <- visualFit(plntsInf ~ aphRel,data=aphids,ncomp=2)
        afit <- mixreg(plntsInf ~ aphRel,data=aphids,thetaStart=vfit$theat)
        plot(vfit,afit,col=c("black","red"),col2=c("blue","green"),
             modnames=c("Visual","Analytic"))
        bfit <- mixreg(plntsInf ~ aphRel,data=aphids)
        plot(afit,bfit,col=c("black","red"),col2=c("blue","green"),
             modnames=c("Visual start","Random start"))
        # Results from visual start and random start look identical.
    
## End(Not run)
    fit.real <- mixreg(plntsInf ~ aphRel,data=aphids,ncomp=2)
    simDat   <- rmixreg(fit.real)
    fit.sim  <- mixreg(plntsInf ~ aphRel,data=simDat,ncomp=2)
    plot(fit.real,fit.sim,col=c("black","red"),col2=c("blue","green"),
         lty=1:2,lty2=3:4,warn=FALSE,legPos="topleft",cMeth="p",pch=20,
         modnames=c("Real data","Simulated data"),
         main="Real data points plotted")
    # We set warn=FALSE since we are well aware that the data sets differ.
    plot(fit.sim,fit.real,col2=c("black","red"),col=c("blue","green"),
         lty2=1:2,lty=3:4,warn=FALSE,legPos="topleft",cMeth="p",pch=20,
         modnames=c("Simulated data","Real data"),
         main="Simuated data points plotted.")

[Package mixreg version 2.0-12 Index]