R/fanplot.R

Defines functions fanplot

Documented in fanplot

######
##  VT::26.11.2021
##
##
##  roxygen2::roxygenise("C:/users/valen/onedrive/myrepo/R/fsdaR", load_code=roxygen2:::load_installed)
##
#' Plots the fan plot for transformation in linear regression
#'
#' @description Plots the fan plot for transformation in linear regression
#'
#' @param out An object of S3 class \code{\link{fsrfan.object}} returned by
#'  \code{\link{fsrfan}} -  a list containing the following elements:
#'    \itemize{
#'        \item \code{Score}: matrix of size \code{(n-init) x length(la)+1} with the fwd search index in
#'          the first column and the value of the score test in each step of the fwd search for \code{la[i]}
#'          in each of the following \code{i} columns.
#'      \item \code{la}: vector containing the values of lambda for which fan plot is constructed.
#'      \item \code{bs}: matrix of size \code{p x length(la)} containing the units forming the initial subset for each value of lambda.
#'      \item \code{Un}: a three-dimensional array containing \code{length(la)} matrices of
#'          size \code{retnUn=(n-init) X retpUn=11}. Each matrix contains
#'          the unit(s) included in the subset at each step in the search associated
#'          with the corresponding element of \code{la}.
#'%%      \item \code{Un}: list of size \code{length(la)}. \code{Un[[i]]} is a \code{(n-init) x 11}
#'%%          matrix which contains the unit(s) included in the subset at each step of the fwd
#'%%          search (necessary only if option datatooltip or databrush are set).
#'      \item \code{y}: a vector containing the response (necessary only if option \code{databrush} is set).
#'      \item \code{X}: a matrix containing the explanatory variables (necessary only if option \code{databrush} is set).
#'   }

#' @param ylim Controls the \code{y} scale in the plot. \code{ylim} is a vector with two elements controlling
#'      minimum and maximum on the \code{y}-axis. Default is to use automatic scale.
#'
#' @param xlim Controls the \code{x} scale in the plot. \code{xlim} is a vector with two elements controlling
#'      minimum and maximum on the \code{x}-axis. Default is to use automatic scale.
#'
#' @param xlab A title for the x axis
#' @param ylab A title for the y axis
#' @param main An overall title for the plot
#' @param lwd The line width, a positive number, default is \code{lwd=1}
#' @param lwd.env Width of the envelope lines. Default is \code{lwd.env=1}.
#' @param cex.lab The magnification to be used for x and y labels relative to the current setting of cex
#' @param cex.axis The magnification to be used for axis annotation relative to the current setting of cex
#'
#' @param tag Plot handle. String which identifies the handle of the plot which is about to be created.
#'    The default is to use tag 'pl_resfwd'. Notice that if the program finds a plot which has
#'    a tag equal to the one specified by the user, then the output of the new plot overwrites
#'    the existing one in the same window else a new window is created.
#'
#' @param datatooltip Interactive clicking. It is inactive if this parameter is set to FALSE.
#'    The default is \code{datatooltip=TRUE}, the user can select with the mouse an individual
#'    mahalanobis distance trajectory in order to have information about the corresponding unit, the associated
#'    label and the step of the search in which the unit enters the subset.
#'    If datatooltip is a list it may contain the following fields:
#'    \enumerate{
#'    \item DisplayStyle determines how the data cursor displays. Possible values are
#'        \code{'datatip'} and \code{'window'} (default). \code{'datatip'} displays
#'        data cursor information in a small yellow text box attached to a black
#'        square marker at a data point you interactively select. \code{'window'}
#'        displays data cursor information for the data point you interactively
#'        select in a floating window within the figure.
#'    \item SnapToDataVertex: specifies whether the data cursor snaps to the nearest data value or
#'        is located at the actual pointer position.
#'        Possible values are \code{SnapToDataVertex='on'} (default) and \code{SnapToDataVertex='off'}.
#'    \item LineColor: controls the color of the trajectory selected with the mouse. It can be an RGB triplet
#'        of values between 0 and 1, or character vector indicating a color name. Note that a RGB vector
#'        can be conveniently chosen with our MATLAB class FSColor, see documentation.
#'    \item SubsetLinesColor: enables to control the color of the trajectories of the units that are
#'        in the subset at a given step of the search (if \code{resfwdplot()} is applied to an
#'        object of class \code{\link{fsreda.object}}) or have a weight greater than 0.9 (if
#'        \code{resfwdplot()} is applied to an object of class \code{\link{sregeda.object}}
#'        or \code{\link{mmregeda.object}}). This can be done (repeatedly) with a left mouse
#'        click in proximity of the step of interest. A right mouse click will terminate the
#'        selection by marking with a up-arrow the step corresponding to the highlighted
#'        lines. The highlighted lines by default are in red, but a different color can be
#'        specified as RGB triplet or character of color name. Note that a RGB vector can
#'        be conveniently chosen with our MATLAB class FSColor, see documentation.
#'        By default \code{SubsetLinesColor=""}, i.e. the modality is not active.
#'        Any initialization for \code{SubsetLinesColor} which cannot be interpreted as
#'        RGB vector will be converted to blue, i.e. \code{SubsetLinesColor} will be forced to be [0 0 1].
#'        If \code{SubsetLinesColor} is not empty the previous option \code{LineColor} is ignored.
#'    }
#'
#' @param label Character vector containing the labels of the units (optional argument used when
#'    \code{datatooltip=TRUE}. If this field is not present labels
#'    row1, ..., rown will be automatically created and included
#'    in the pop up datatooltip window).
#' @param nameX Add variable labels in plot. A vector of strings of length \code{p}
#'  containing the labels of the variables in the dataset.
#'  If it is empty (default) the sequence \code{X1, ..., Xp} will be created automatically
#'
#' @param databrush Interactive mouse brushing. If databrush is missing or empty (default), no brushing is done.
#'  The activation of this option (databrush is \code{TRUE} or a list) enables the user to select
#'    a set of trajectories in the current plot and to see them highlighted in the scatterplot matrix.
#'    If the scatterplot matrix does not exist it is automatically created.
#'    In addition, brushed units can be highlighted in the monitoring mahalanobis distance plot.
#'    Note that the window style of the other figures is set equal to that which contains the monitoring mahalanobis distance plot.
#'    In other words, if the monitoring mahalanobis distance plot is docked all the other figures will be docked too.
#'
#'    If \code{databrush=TRUE} the default selection tool is a rectangular brush and it is
#'    possible to brush only once (that is persist='').
#'
#'    If \code{databrush=list(...)}, it is possible to use all optional arguments of
#'    the MATLAB function \code{selectdataFS()} and the following optional arguments:
#'    \itemize{
#'    \item persist. Persist is an empty value or a character containing 'on' or 'off'.
#'        The default value is \code{persist=""}, that is brushing is allowed only once.
#'        If \code{persist="on"} or \code{persis="off"} brushing can be done as many time as
#'        the user requires. If \code{persist='on'} then the unit(s) currently brushed are
#'        added to those previously brushed. It is possible, every time a new brushing is
#'        done, to use a different color for the brushed units. If \code{persist='off'}
#'        every time a new brush is performed units previously brushed are removed.
#'    \item \code{label}: add labels of brushed units in the monitoring plot.
#'    \item \code{labeladd}: add labels of brushed units in the scatterplot matrix.
#'        If this option is '1', we label the units of the last selected group with
#'        the unit row index in the matrix X. The default value is labeladd='',
#'        i.e. no label is added.
#'    }
#'
#' @param conflev confidence interval for the horizontal bands. It can be a vector of
#'  different confidence level values, e.g. c(0.95, 0.99, 0.999).
#'  The confidence interval is based on the $chi^2$ distribution.
#'
#' @param trace Whether to print intermediate results. Default is \code{trace=FALSE}.
#'
#' @param ... potential further arguments passed to lower level functions.

#' @return  none

#' @references
#'  Atkinson, A.C. and Riani, M. (2000), \emph{Robust Diagnostic Regression Analysis} Springer Verlag, New York.
#'
#'  Atkinson, A.C. and Riani, M. (2002), Tests in the fan plot for robust, diagnostic transformations in regression,
#'      \emph{Chemometrics and Intelligent Laboratory Systems}, \bold{60}, pp. 87--100.
#'
#'  Atkinson, A.C. Riani, M. and  Corbellini A. (2019), The analysis of transformations for profit-and-loss data,
#'      \emph{Journal of the Royal Statistical Society, Series C, "Applied Statistics"}, \bold{69}, pp. 251--275.
#'      \doi{10.1111/rssc.12389}
#'
#'  Atkinson, A.C. Riani, M. and Corbellini A. (2021), The Box-Cox Transformation: Review and Extensions,
#'      \emph{Statistical Science}, \bold{36}(2), pp. 239--255. \doi{10.1214/20-STS778}.
#'

#' @export
#' @author FSDA team, \email{valentin.todorov@@chello.at}


fanplot <- function(out,
        xlab="Subset of size m", ylab="Score test statistic", main="Fan plot",
        xlim, ylim, cex.lab, cex.axis, lwd=2, lwd.env=1,
        tag, datatooltip, label, nameX, databrush, conflev,
        trace=FALSE, ...) {

    ## Perform check on the structure of 'out'
    if(missing(out) | !inherits(out, "fsrfan"))
        stop("Function defined only for monitoring type output.")

    ## The needed elements are MAL and Y=X, and Un (FS) or bdp (S) or eff (MM).
    ## The R class name is mapped to a Matlab class name
    outStr <- list(Score=out$Score, X=out$X, y=out$y, class=getMatlabClass(class(out)))

    control <- list(...)

##  Graphical parameters
    if(!missing(xlab))
        control$labx <- xlab
    if(!missing(ylab))
        control$laby <- ylab
    if(!missing(main))
        control$titl <- main
    if(!missing(xlim))
        control$xlimx <- xlim
    if(!missing(ylim))
        control$ylimy <- ylim
    if(!missing(cex.lab)) {
        control$FontSize <- 12  ## the default
        control$FontSize <- cex.lab * control$FontSize
    }
    if(!missing(cex.axis)) {
        control$SizeAxesNum <- 12  ## the default
        control$SizeAxesNum <- cex.axis * control$SizeAxesNum
    }

    control$lwd <- lwd
    control$lwdenv <- lwd.env

    if(!missing(tag))
        control$tag <- as.character(tag)

    if(!missing(datatooltip)) {
        if(is.logical(datatooltip))
            control$datatooltip <- ifelse(datatooltip, 1, "")
        else if(is.list(datatooltip))
            control$datatooltip <- datatooltip
        else
            stop(paste("Wrong argument 'datatooltip':", datatooltip))
    }
    
    if(!missing(label))
        control$label <- label
    if(!missing(nameX))
        control$nameX <- nameX

    if(!missing(databrush)) {
        if(is.logical(databrush))
        {
            if(databrush)
                control$databrush <- 1
        }else if(is.list(databrush))
            control$databrush <- databrush
        else
            stop(paste("Wrong argument 'databrush':", databrush))
    }

    if(!missing(conflev)) {
        control$conflev <- conflev
    } else {
        control$conflev <- 0.975
    }

    if(trace) {
        cat("\nOptional parameters to fanplot(): \n")
        print(control)
    }

    # Initialize parlist to the input values. All parameters from the control structure
    # must be added to the R list *before* converting it into a MATLAB struct

    ## VT::16.10.2017: the mandatory input parameters are all contained
    ##  in an S3 class (a list), which is the output of one of the XXXeda() fucntions.
    ##  Convert the S3 class to a Matlab structure and initialize with it the parameter list.
    ##  NOTE: this is identical to the initialization of the parameter list in fsreg()
    ##      with the mandatory X and y.
    ##
    parlist <- list(list2MatlabStruct(outStr))

    paramNames = names(control)
    if (length(paramNames) > 0) {
        for (i in 1:length(paramNames)) {
            paramName = paramNames[i];
            paramValue = control[[i]];

            ##matlabValue = rType2MatlabType(paramName, paramValue, forceStringsToCellArray = TRUE)
            matlabValue = rType2MatlabType(paramName, paramValue, forceStringsToCellArray = FALSE)

            parlist = c(parlist, .jnew("java/lang/String", paramName), matlabValue)
        }
    }

    callFsdaFunction("fanplot", "[Ljava/lang/Object;", 1, parlist)
    return(invisible(NULL))
}

Try the fsdaR package in your browser

Any scripts or data that you put into this service are public.

fsdaR documentation built on May 20, 2026, 1:07 a.m.