[R] (no subject)

Ethan Brown ethancbrown at gmail.com
Tue Jun 21 06:03:08 CEST 2011


Hi Ungku, your error is in your call to lda.default; the object that
you want to plot, lda_result, is thus not created and cannot be
plotted.

The error you're getting with LDA doesn't look to be a coding error,
but a statistical one. I don't know anything about LDA, but simply
googling "variablest constant within results" came up with several
useful links that explain the problem. Have you looked at these? The
first I found was this:
http://miner.chem.purdue.edu/Lectures/Lecture10.pdf , which explains
the related theory behind this error, and
http://r.789695.n4.nabble.com/Linear-Discriminant-Analysis-error-quot-Variables-appear-constant-quot-td3527288.html

The second link, by the way, is an example of an R question with a
clear subject line and description of the problem, making it much
easier for us to help.

I notice that you have already asked questions related to this same
function and error message in this list, and have received some
suggestions from people who know about LDA. Have you tried these
suggestions? I don't see any trace of them in your code.

Best,
Ethan

P.S. Please include the r-help list in the to: field in any follow-up
emails so other people can chime in or learn from this exchange.

On Mon, Jun 20, 2011 at 7:50 PM, Ungku Akashah <kaslah90 at yahoo.com> wrote:
>
> hello,
> i already include the error in blue color word.
> i hope it can help you to understand my question.
> if not burden you, please give me a guide how to correct the error or maybe you can correct the coding cause error.
> thank you.
>
>
> > #    lda.r
> > #
> > #    Author:    Amsha Nahid, Jairus Bowne, Gerard Murray
> > #    Purpose:    Perform Linear Discriminant Analysis (LDA)
> > #
> > #    Input:    Data matrix as specified in Data-matrix-format.pdf
> > #    Output:    LDA plot
> > #
> > #    Notes:    Missing values (if any) are replaced by the half of the lowest
> > #              value in the entire data matrix.
> >
> >
> > #
> > #    Load necessary libraries, and install them if they are missing
> > #
> > tryCatch(library(MASS), error=function(err)
> +     # if this produces an error:
> +     install.packages("MASS",repos="http://cran.ms.unimelb.edu.au/"))
> >
> > #
> > #    Prepare the data matrix
> > #
> > # Read in the .csv file
> > data<-read.csv("C:/Users/nadya/Desktop/praktikal UTM/TASK2/new(40data)S2 100 EMS EPI 300-399.csv", sep=",", row.names=1, header=TRUE)
> > # Get groups information
> > groups<-data[,1]
> > # Remove groups for data processing
> > lda_data<-data[,-1]
> > # Replace any missing values (see Notes)
> > lda_data[is.na(lda_data)]<-0.5*(min(lda_data,na.rm=TRUE))
> >
> > #
> > #    Perform the LDA
> > #
> > lda_result<-lda(lda_data,groups)
> Error in lda.default(x, grouping, ...) :
>   variables  1  3  5  8 10 15 17 20 27 29 34 appear to be constant within groups
> >
> > #
> > #    Generate the figures (on screen)
> > #
> > #    Image generation - function definition
> > pic_onscr<-function(matrix, title, cex_val=1)
> +     {x11()
> +     par(mgp=c(5,2,0),                           # axis margins
> +                                                 # (title, labels, line)
> +         mar=c(7,4,4,2),                         # plot margins (b,l,t,r)
> +         las=1)                                  # horizontal labels
> +     plot(matrix,                                # data to plot
> +         cex=cex_val,                            # font size
> +         dimen=2                                 # dimensions to plot
> +         )
> +     title(main=title)                           # title of plot
> +     }
> > # Plot LDA scores with sample names
> > pic_onscr(lda_result,"Linear Discriminant Analysis")
> Error in plot(matrix, cex = cex_val, dimen = 2) :
>   error in evaluating the argument 'x' in selecting a method for function 'plot': Error: object 'lda_result' not found
> > # For plotting with larger font size, use a different value of cex:
> > # pic_onscr(lda_result, "LDA Plot", dimen=2, cex=3)
> >
> > #
> > #    Generate figures as image files
> > #
> > #    (Uncomment blocks as necessary)
> >
> > ##### jpeg #####
> > # pic_jpg<-function(filename, matrix, title, cex_val=1)
> > #     {# Start jpeg device with basic settings
> > #     jpeg(filename,
> > #         quality=100,                            # image quality (percent)
> > #         bg="white",                             # background colour
> > #         res=300,                                # image resolution (dpi)
> > #         units="in", width=8.3, height=5.8       # image dimensions (inches)
> > #         )
> > #     par(mgp=c(5,2,0),                           # axis margins
> > #                                                 #  (title, labels, line)
> > #         mar=c(7,4,4,2),                         # plot margins (b,l,t,r)
> > #         las=1                                   # horizontal labels
> > #         )
> > #     # Draw the plot
> > #     plot(matrix,                                # data to plot
> > #         cex=cex_val,                            # font size
> > #         dimen=2                                 # dimensions to plot
> > #         )
> > #     title(main=title)                           # title of plot
> > #
> > #     dev.off()
> > #     }
> > # pic_jpg("LDA.jpg", lda_result, "Linear Discriminant Analysis")
> > ##### end jpeg #####
> >
> > ##### png #####
> > # pic_png<-function(filename, matrix, title, cex_val=1)
> > #     {# Start png device with basic settings
> > #     png(filename,
> > #         bg="white",                             # background colour
> > #         res=300,                                # image resolution (dpi)
> > #         units="in", width=8.3, height=5.8       # image dimensions (inches)
> > #         )
> > #     par(mgp=c(5,2,0),                           # axis margins
> > #                                                 #  (title, labels, line)
> > #         mar=c(7,4,4,2),                         # plot margins (b,l,t,r)
> > #         las=1                                   # horizontal labels
> > #         )
> > #     # Draw the plot
> > #     plot(matrix,                                # data to plot
> > #         cex=cex_val,                            # font size
> > #         dimen=2                                 # dimensions to plot
> > #         )
> > #     title(main=title)                           # title of plot
> > #
> > #     dev.off()
> > #     }
> > # pic_png("LDA.png", lda_result, "Linear Discriminant Analysis")
> > ##### end png #####
> >
> > ##### tiff #####
> > # pic_tiff<-function(filename, matrix, title, cex_val=1)
> > #     {# Start tiff device with basic settings
> > #     tiff(filename,
> > #         bg="white",                             # background colour
> > #         res=300,                                # image resolution (dpi)
> > #         units="in", width=8.3, height=5.8,      # image dimensions (inches)
> > #         compression="none"                      # image compression
> > #                                                 #  (one of none, lzw, zip)
> > #         )
> > #     par(mgp=c(5,2,0),                           # axis margins
> > #                                                 #  (title, labels, line)
> > #         mar=c(7,4,4,2),                         # plot margins (b,l,t,r)
> > #         las=1                                   # horizontal labels
> > #         )
> > #     # Draw the plot
> > #     plot(matrix,                                # data to plot
> > #         cex=cex_val,                            # font size
> > #         dimen=2                                 # dimensions to plot
> > #         )
> > #     title(main=title)                           # title of plot
> > #
> > #     dev.off()
> > #     }
> > # pic_tiff("LDA.tif", lda_result, "Linear Discriminant Analysis")
> > ##### end tiff #####
> >
>
> ________________________________
> From: Ethan Brown <ethancbrown at gmail.com>
> To: Ungku Akashah <kaslah90 at yahoo.com>; r-help at r-project.org
> Sent: Tue, June 21, 2011 5:48:55 AM
> Subject: Re: [R] (no subject)
>
> Hi Ungku, it's really difficult for us to take a huge block of code and understand where an error happened. There's several things that can help us help you:
>
> 1) First and foremost, what is the error message or undesired behavior you're experiencing?
> 2) Second, please pare down the code to the place where you're experiencing a problem. Maybe just generate some simple data and try making the plot from there without all the calculations and formatting options, and see if it works then. If it still doesn't, post that simplified code and someone here will be much more able to help you.
>
> The posting guide, http://www.R-project.org/posting-guide.html, has some further tips.
>
> Best,
> Ethan
>
> On Sun, Jun 19, 2011 at 7:05 PM, Ungku Akashah <kaslah90 at yahoo.com> wrote:
>>
>> HELLO, anybody... could you help me to check the below coding for volcano.
>> what is the mistake?
>> what the plot could not display?
>>
>>
>>
>>
>>
>>
>>
>> #    volcano_plot.r
>> #
>> #    Author:    Amsha Nahid, Jairus Bowne, Gerard Murray
>> #    Purpose:    Produces a volcano plot
>> #
>> #    Input:    Data matrix as specified in Data-matrix-format.pdf
>> #    Output:    Plots log2(fold change) vs log10(t-test P-value)
>> #
>> #    Notes:    Group value for control must be alphanumerically first
>> #              Script will return an error if there are more than 2 groups
>>
>> #
>> #    Load the data matrix
>> #
>> # Read in the .csv file
>> data<-read.csv("input5.csv", sep=",", row.names=1, header=TRUE)
>> # Get groups information
>> groups<-data[,1]
>> # Get levels for groups
>> grp_levs<-levels(groups)
>> if (length(levels(groups)) > 2)
>>    print("Number of groups is greater than 2!") else {
>>
>>    #
>>    #    Split the matrix by group
>>    #
>>    new_mats<-c()
>>    for (ii in 1:length(grp_levs))
>>        new_mats[ii]<-list(data[which(groups==levels(groups)[ii]),])
>>
>>    #
>>    #    Calculate the means
>>    #
>>    # For each matrix, calculate the averages per column
>>    submeans<-c()
>>    # Preallocate a matrix for the means
>>    means<-matrix(
>>        nrow = 2,
>>        ncol = length(colnames(data[,-1])),
>>        dimnames = list(grp_levs,colnames(data[,-1]))
>>        )
>>    # Calculate the means for each variable per sample
>>    for (ii in 1:length(new_mats))
>>        {submeans[ii]<-list(apply(new_mats[[ii]][,-1],2,mean,na.rm=TRUE))
>>        means[ii,]<-submeans[[ii]]}
>>
>>    #
>>    #    Calculate the fold change
>>    #
>>    folds<-matrix(
>>        nrow=length(means[,1]),
>>        ncol=length(means[1,]),
>>        dimnames=list(rownames(means),colnames(means))
>>        )
>>    for (ii in 1:length(means[,1]))
>>        for (jj in 1:length(means[1,]))
>>            folds[ii,jj]<-means[ii,jj]/means[1,jj]
>>
>>    #
>>    #    t-test P value data
>>    #
>>
>> pvals<-matrix(nrow=ncol(data[,-1]),ncol=1,dimnames=list(colnames(data[-1]),"P-Value"))
>>
>>
>>    #
>>    #    Perform the t-Test
>>    #
>>    for(ii in 1:nrow(pvals)) {
>>        pvals[ii,1]<-t.test(new_mats[[1]][,ii+1],new_mats[[2]][,ii+1])$p.value
>>        }
>>
>>    m<-length(pvals)
>>    x_range<-range(c(
>>        min(
>>            range(log2(folds[2,])),
>>            range(c(-1.5,1.5))
>>            ),
>>        max(
>>            range(log2(folds[2,])),
>>            range(c(-1.5,1.5))
>>            )
>>        ))
>>    y_range<-range(c(
>>        min(range(-log10(pvals)),
>>            range(c(0,2))
>>            ),
>>        max(range(-log10(pvals)),
>>            range(c(0,2))
>>            )
>>        ))
>>
>>    #
>>    #    Plot data
>>    #
>>    # Define a function, since it's rather involved
>>    volcano_plot<-function(fold, pval)
>>        {plot(x_range,                                 # x-dim
>>            y_range,                                   # y-dim
>>            type="n",                                  # empty plot
>>            xlab="log2 Fold Change",                   # x-axis title
>>            ylab="-log10 t-Test P-value",              # y-axis title
>>            main="Volcano Plot",                       # plot title
>>            )
>>            abline(h=-log10(0.05),col="green",lty="44")# horizontal line at
>> P=0.05
>>            abline(v=c(-1,1),col="violet",lty="1343")  # vertical lines at
>> 2-fold
>>            # Plot points based on their values:
>>            for (ii in 1:m)
>>                # If it's below 0.05, we're not overly interested: purple.
>>                if (-log10(pvals[ii])>(-log10(0.05))) {
>>                    # Otherwise, more checks;
>>                    # if it's greater than 2-fold decrease: blue
>>                    if (log2(folds[2,][ii])>(-1)) {
>>                        # If it's significant but didn't change much: orange
>>                        if (log2(folds[2,][ii])<1) {
>>                            points(
>>                                log2(folds[2,][ii]),
>>                                -log10(pvals[ii]),
>>                                col="orange",
>>                                pch=20
>>                                )
>>                            # Otherwise, greater than 2-fold increase: red
>>                            } else {
>>                                points(
>>                                    log2(folds[2,][ii]),
>>                                    -log10(pvals[ii]),
>>                                    col="red",
>>                                    pch=20
>>                                    )
>>                            }
>>                        } else {
>>                            points(
>>                                log2(folds[2,][ii]),
>>                                -log10(pvals[ii]),
>>                                col="blue",
>>                                pch=20
>>                                )
>>                            }
>>                    } else {
>>                        points(
>>                            log2(folds[2,][ii]),
>>                            -log10(pvals[ii]),
>>                            col="purple",
>>                            pch=20
>>                            )
>>                    }
>>        }
>>    # Plot onscreen via function
>>    x11()
>>    volcano_plot(folds,pvals)
>>
>>    # Return table to analyse results
>>
>>    #
>>    #    Generate figures as image files
>>    #
>>    #    (Uncomment blocks as necessary)
>>
>>    ##### jpeg #####
>>    # pic_jpg<-function(filename, fold, pval)
>>    #     {# Start jpeg device with basic settings
>>    #     jpeg(filename,
>>    #         quality=100,                           # image quality (percent)
>>    #         bg="white",                            # background colour
>>    #         res=300,                               # image resolution (dpi)
>>    #         units="in", width=8.3, height=5.8)     # image dimensions (inches)
>>    #     par(mgp=c(5,2,0),                          # axis margins
>>    #                                                # (title, labels, line)
>>    #         mar=c(6,6,4,2),                        # plot margins (b,l,t,r)
>>    #         las=1                                  # horizontal labels
>>    #         )
>>    #     # Draw the plot
>>    #     volcano_plot(folds, pvals)
>>    #     dev.off()
>>    #     }
>>    # pic_jpg("volcano_plot.jpg")
>>    ##### end jpeg #####
>>
>>
>>    #### png #####
>>    # pic_png<-function(filename, fold, pval)
>>    #     {# Start png device with basic settings
>>    #     png(filename,
>>    #         bg="white",                            # background colour
>>    #         res=300,                               # image resolution (dpi)
>>    #         units="in", width=8.3, height=5.8)     # image dimensions (inches)
>>    #     par(mgp=c(5,2,0),                          # axis margins
>>    #                                                # (title, labels, line)
>>    #         mar=c(6,6,4,2),                        # plot margins (b,l,t,r)
>>    #         las=1                                  # horizontal labels
>>    #         )
>>    #     # Draw the plot
>>    #     volcano_plot(folds, pvals)
>>    #     dev.off()
>>    #     }
>>    # pic_png("volcano_plot.png")
>>    #### end png #####
>>
>>
>>    # #### tiff #####
>>    # pic_tiff<-function(filename, fold, pval)
>>    #     {# Start tiff device with basic settings
>>    #     tiff(filename,
>>    #         bg="white",                            # background colour
>>    #         res=300,                               # image resolution (dpi)
>>    #         units="in", width=8.3, height=5.8)     # image dimensions (inches)
>>    #         compression="none"                     # image compression
>>    #                                                #  (one of none, lzw, zip)
>>    #     par(mgp=c(5,2,0),                          # axis margins
>>    #                                                # (title, labels, line)
>>    #         mar=c(6,6,4,2),                        # plot margins (b,l,t,r)
>>    #         las=1                                  # horizontal labels
>>    #         )
>>    #     # Draw the plot
>>    #     volcano_plot(folds, pvals)
>>    #     dev.off()
>>    #     }
>>    # pic_tiff("volcano_plot.tif")
>>    # #### end tiff #####
>>
>>
>>
>>
>>    #
>>    #    Legacy code which allows for blue/red to be independent of 0.05
>>    #    (purple is limited to the middle lower region)
>>    #
>>    #####
>>    #     for (ii in 1:m)
>>    #         if (log2(folds[2,][ii])<1) {
>>    #             if (log2(folds[2,][ii])>-1) {
>>    #                 if (-log10(pvals[ii])<(-log10(0.05))) {
>>    #                     points(
>>    #                         log2(folds[2,][ii]),
>>    #                         -log10(pvals[ii]),
>>    #                         col="purple",
>>    #                         pch=20
>>    #                         )
>>    #                     } else {
>>    #                         points(
>>    #                             log2(folds[2,][ii]),
>>    #                             -log10(pvals[ii]),
>>    #                             col="orange",
>>    #                             pch=20
>>    #                             )
>>    #                     }
>>    #                 } else {
>>    #                     points(
>>    #                         log2(folds[2,][ii]),
>>    #                         -log10(pvals[ii]),
>>    #                         col="blue",
>>    #                         pch=20
>>    #                         )
>>    #                     }
>>    #             } else {
>>    #                 points(
>>    #                     log2(folds[2,][ii]),
>>    #                     -log10(pvals[ii]),
>>    #                     col="red",
>>    #                     pch=20
>>    #                     )
>>    #             }
>>
>> # If function from above needs to be closed
>> }
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list