+ Reply to Thread
Results 1 to 10 of 10

Thread: obtaining numeric correlations within scatterplot matrix(pairs)?

  1. #1
    Points: 70, Level: 1
    Level completed: 40%, Points required for next Level: 30

    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    obtaining numeric correlations within scatterplot matrix(pairs)?



    Hi all,
    I have created scatterplot matrixes for my data but cannot for the life of me figure out how to obtain and then insert the correlations in the matrix. I had read posts online but I am generating errors. My pairs formula is
    pairs(~glass_sponge+Depth+S+M+Sm+P,data=Gs)


    I looked at the R help file and just tried to apply what they wrote, and tried...
    panel.cor <- function(x, y, digits=2, prefix="", cex.cor, ...)
    {
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    r <- abs(cor(x, y))
    txt <- format(c(r, 0.123456789), digits=digits)[1]
    txt <- paste(prefix, txt, sep="")
    if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
    text(0.5, 0.5, txt, cex = cex.cor * r)
    }
    pairs(Gs, upper.panel=panel.cor)


    this doesnt work...which isnt surprising because I do not know what all the number mean in the example...what do I need to modify to make this error not occur? Or better yet, just make my own correlations not using this R example?

    I have attached the plot. Your help is appreciated, I have spent an afternoon trying to sort this out and I am sure it cannot be so difficult, but I am definitely missing something here.

    Best Regards.
    Attached Images  

  2. #2
    FormerlyKnownAsRaptor
    Points: 24,451, Level: 95
    Level completed: 11%, Points required for next Level: 899
    Awards:
    Activity Award
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,174
    Thanks
    884
    Thanked 552 Times in 500 Posts

    Re: obtaining numeric correlations within scatterplot matrix(pairs)?

    Hi snowsoc11

    You haven't provided a data set to work with and thus it's more difficult to assist with your problem. For more about how to provide a reproducible example check out:

    http://stackoverflow.com/questions/5...ucible-example

    It's also much easir to help when you place code inside of code tags. You can do this by either clicking the pound (#) sign icon or wrap with [CODE]some code[/CODE] which produces:
    Code: 
    some code
    Once you do that it'll be easier to help you. In the mean time may I suggest the pairs.panels function from the psych package. I think it's more informative visually and gives you the correlations you're after. Here's an example:
    Code: 
    library(psych)
    pairs.panels(attitude)
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  3. #3
    Points: 70, Level: 1
    Level completed: 40%, Points required for next Level: 30

    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: obtaining numeric correlations within scatterplot matrix(pairs)?

    thank you for your reply! I am sorry it is so obvious this is my first time on the forum. I actually tried the library pysch package and my R (r studio console) said the psych package doesnt exist...not sure what is going on there.
    I did my best with trying to understand the link you sent, I attached my dput output of my data set, maybe that will help? The main goal of this is to find correlations in environmental variables (predict) in regards to species densities (response). I am sorry if the attachment is not at all what you meant in providing data, I hope it is!
    Attached Files

  4. #4
    Cookie Scientist
    Points: 5,945, Level: 49
    Level completed: 98%, Points required for next Level: 5
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    797
    Thanks
    18
    Thanked 315 Times in 241 Posts

    Re: obtaining numeric correlations within scatterplot matrix(pairs)?

    You need to install the psych package first before you can load it, using:
    Code: 
    install.packages("psych")
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

  5. #5
    Points: 70, Level: 1
    Level completed: 40%, Points required for next Level: 30

    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: obtaining numeric correlations within scatterplot matrix(pairs)?

    Great thank you. After trying the psych method and pasting the formula I posted above originally, I get the error Error in plot.new() : figure margins too large and that is kind of the root of my problem, if that helps determine what I must adjust.

  6. #6
    TS Contributor
    Points: 6,575, Level: 53
    Level completed: 13%, Points required for next Level: 175
    Lazar's Avatar
    Location
    Sydney
    Posts
    665
    Thanks
    110
    Thanked 165 Times in 150 Posts

    Re: obtaining numeric correlations within scatterplot matrix(pairs)?

    try putting in dev.off(). This should fix the problem.

  7. #7
    Points: 70, Level: 1
    Level completed: 40%, Points required for next Level: 30

    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: obtaining numeric correlations within scatterplot matrix(pairs)?

    Hi Lazar. I put dev.off() at the end of my
    panel.cor <- function(x, y, digits=2, prefix="", cex.cor, ...)
    {
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    r <- abs(cor(x, y))
    txt <- format(c(r, 0.123456789), digits=digits)[1]
    txt <- paste(prefix, txt, sep="")
    if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
    text(0.5, 0.5, txt, cex = cex.cor * r)
    dev.off()
    }
    pairs(Gs, upper.panel=panel.cor)

    and it is still generating the error...I dont think R and I get along too well. I may just go do all of the correlations manually, blah.

  8. #8
    FormerlyKnownAsRaptor
    Points: 24,451, Level: 95
    Level completed: 11%, Points required for next Level: 899
    Awards:
    Activity Award
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,174
    Thanks
    884
    Thanked 552 Times in 500 Posts

    Re: obtaining numeric correlations within scatterplot matrix(pairs)?

    @snowsoc11

    It's a little bit of a learning curve but so worth it.

    We're having a hard time helping you because we don't have a data set to work with (and less detrimental but still hindering your response you're not using code tags for code and data sets). Please see my comments above on how to put your code in code tags. To give us a data set to work with use the following code and post it back here:

    Code: 
    dput(head(Gs))
    I assume your data set is called Gs because that's what you're feeding to the pairs function.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  9. #9
    TS Contributor
    Points: 6,575, Level: 53
    Level completed: 13%, Points required for next Level: 175
    Lazar's Avatar
    Location
    Sydney
    Posts
    665
    Thanks
    110
    Thanked 165 Times in 150 Posts

    Re: obtaining numeric correlations within scatterplot matrix(pairs)?

    Sorry snow I did not mean for you to add dev.off to your code. What sometimes happens is that if you have used some device previously (e.g. pdf, svg, etc.) and forgot to use dev.off you will get the error you got. I was suggesting you use dev.off to reset the plots.

  10. #10
    FormerlyKnownAsRaptor
    Points: 24,451, Level: 95
    Level completed: 11%, Points required for next Level: 899
    Awards:
    Activity Award
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,174
    Thanks
    884
    Thanked 552 Times in 500 Posts

    Re: obtaining numeric correlations within scatterplot matrix(pairs)?


    I can run your code with the data set you provided. Try Lazar's suggestions.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts








Advertise on Talk Stats