[R] scree plot

Mulholland, Tom Tom.Mulholland at dpi.wa.gov.au
Wed Jan 5 03:29:13 CET 2005


I thought this would be simple enough and in some ways it is, but I don't have an answer I would call easy

The manual process would be to use barplot instead of screeplot, so that you could return the x values. The other choice would be to hack the screeplot function to return the values. (see the end)

Then you need to get the y values. If you follow the example in screeplot

## The variances of the variables in the
     ## USArrests data vary by orders of magnitude, so scaling is appropriate
     (pc.cr <- princomp(USArrests, cor = TRUE))  # inappropriate
     screeplot(pc.cr)

 then you might use
   pc.cr$sdev^2 * 0.9 

My difficulty came when I looked at pc.cr$loadings. This uses the print.loadings method to calulate the cumulative values

a very quick kludge gives me

cumvar <- function (x, digits = 3, cutoff = 0.1, sort = FALSE, ...) 
{
    Lambda <- unclass(x)
    p <- nrow(Lambda)
    factors <- ncol(Lambda)
    if (sort) {
        mx <- max.col(abs(Lambda))
        ind <- cbind(1:p, mx)
        mx[abs(Lambda[ind]) < 0.5] <- factors + 1
        Lambda <- Lambda[order(mx, 1:p), ]
    }
    cat("\nLoadings:\n")
    fx <- format(round(Lambda, digits))
    names(fx) <- NULL
    nc <- nchar(fx[1])
    fx[abs(Lambda) < cutoff] <- paste(rep(" ", nc), collapse = "")
    vx <- colSums(x^2)
    varex <- rbind("SS loadings" = vx)
    if (is.null(attr(x, "covariance"))) {
        varex <- rbind(varex, "Proportion Var" = vx/p)
        if (factors > 1) 
            varex <- rbind(varex, "Cumulative Var" = cumsum(vx/p))
    }
    invisible(return(cumsum(vx/p)))
}


screeplot <- function (x, npcs = min(10, length(x$sdev)), type = c("barplot",
    "lines"), main = deparse(substitute(x)), ...)
{
    main
    type <- match.arg(type)
    pcs <- x$sdev^2
    xp <- seq(length = npcs)
    if (type == "barplot")
        tt <- barplot(pcs[xp], names = names(pcs[xp]), main = main,
            ylab = "Variances", ...)
    else {
        plot(xp, pcs[xp], type = "b", axes = FALSE, main = main,
            xlab = "", ylab = "Variances", ...)
        axis(2)
        axis(1, at = xp, labels = names(pcs[xp]))
    }
    invisible(return(tt))
    
}

here <- screeplot(pc.cr)
text(here,pc.cr$sdev^2 + 0.1,paste(cumvar(pc.cr$loadings)*100,"%",sep = ""),xpd=T)



> -----Original Message-----
> From: Anne [mailto:anne.piotet at urbanet.ch]
> Sent: Wednesday, 5 January 2005 5:45 AM
> To: R list
> Subject: [R] scree plot
> 
> 
> Hi!
> 
> Is there an easy way to add to the scree-plot labels to each 
> value pertaining to the cumulative proportion of explained variance?
> 
> Thanks and a happy new year
> 
> Anne
> ----------------------------------------------------
> Anne Piotet
> Tel: +41 79 359 83 32 (mobile)
> Email: anne.piotet at m-td.com
> ---------------------------------------------------
> M-TD Modelling and Technology Development
> PSE-C
> CH-1015 Lausanne
> Switzerland
> Tel: +41 21 693 83 98
> Fax: +41 21 646 41 33
> --------------------------------------------------
>  
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list