+ Reply to Thread
Results 1 to 4 of 4

Thread: Basic question- Compute correlation with many time-series

  1. #1
    Points: 136, Level: 2
    Level completed: 72%, Points required for next Level: 14

    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Basic question- Compute correlation with many time-series



    Hi. Very basic question, but I am looking to find the correlation coefficient (r, not r-squared, I need to know the sign) between a time-series A and all of the time-series embedded within the matrix Z (which might for example be a 90 x 100 matrix of temperature measurements, with 90 measured values at 100 different locations). A would then be of length 90 as well.

    The desired output would be a list of 100 correlation values. I've tried using "lm" and cor functions (e.g., cor(A,Z)), but it didn't work whenever Z had more than one time series.

  2. #2
    Bhoot
    Points: 1,270, Level: 19
    Level completed: 70%, Points required for next Level: 30

    Posts
    1,758
    Thanks
    40
    Thanked 124 Times in 106 Posts

    Re: Basic question- Compute correlation with many time-series

    Cor work for me. Try this
    Code: 
    A=rnorm(90)
    Z= matrix(rnorm(90*100),nrow=90)
    cor(A,Z)

  3. #3
    Points: 136, Level: 2
    Level completed: 72%, Points required for next Level: 14

    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Basic question- Compute correlation with many time-series

    it works fine now...thanks

  4. #4
    Points: 1,980, Level: 26
    Level completed: 80%, Points required for next Level: 20
    derksheng's Avatar
    Posts
    247
    Thanks
    49
    Thanked 34 Times in 28 Posts

    Re: Basic question- Compute correlation with many time-series


    If u also need p-value

    Code: 
    install.packages("Hmisc")
    require(Hmisc)
    sapply(1:ncol(matrix2),function(i) rcorr(vector,matrix2[,i])$r[1,2] ) #r
    sapply(1:ncol(matrix2),function(i) rcorr(vector,matrix2[,i])$P[1,2] ) #Pvalue

+ Reply to 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