[R] How to calculate correlation of a vector in R?
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Mon Nov 3 03:20:42 CET 2014
k <- sigma^2 * exp( -1/(2*l^2) * outer( v,v,FUN=function(x,y){(x-y)^2}))
but perhaps you should look at the e1071 package instead?
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On November 2, 2014 4:19:56 PM PST, C W <tmrsg11 at gmail.com> wrote:
>Thanks, Jeff. I had some misunderstanding.
>
>So, I want to calculate the squared exponential of vector v
>
>v = c(700, 800, 1029)
>
>formula is:
>k(x_i, x_j)=sigma^2 * exp(-1/(2*l^2) * (x_i - x_j) ^2)
>
>where,
>sigma=7, l=100
>
>
>I used,
>> v <- c(700, 800, 1029)
>> corr.matrix(cbind(v),scales=0.5)
> [,1] [,2] [,3]
>[1,] 1 0 0
>[2,] 0 1 0
>[3,] 0 0 1
>
>the output should be covariance matrix = [49, 29.7, 0.02; 29.7, 49,
>3.6;
>0.2, 3.6, 49]
>
>
>
>On Sun, Nov 2, 2014 at 7:04 PM, Jeff Newmiller
><jdnewmil at dcn.davis.ca.us>
>wrote:
>
>> What is your question? The matrix form is probably what you are
>looking
>> for, but you put the same vector in three times so if course it is
>all
>> ones. I don't know what you expected to happen when you entered
>cor(v)
>> since there is nothing to correlate if you only have one vector.
>>
>> Please post in plain text as the footer and Posting Guide request.
>>
>---------------------------------------------------------------------------
>> Jeff Newmiller The ..... ..... Go
>Live...
>> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
>> Go...
>> Live: OO#.. Dead: OO#..
>Playing
>> Research Engineer (Solar/Batteries O.O#. #.O#. with
>> /Software/Embedded Controllers) .OO#. .OO#.
>rocks...1k
>>
>---------------------------------------------------------------------------
>> Sent from my phone. Please excuse my brevity.
>>
>> On November 2, 2014 3:30:14 PM PST, C W <tmrsg11 at gmail.com> wrote:
>> >Hi list,
>> >I have trying to calculate the covariance/correlation of three
>> >elements. I
>> >have vector say,
>> >
>> >v <- c(700, 800, 1000)
>> >
>> >I want to have a 3 by 3 correlation matrix, meaning cor(v1, c2),
>> >cor(v1,
>> >c3), cor(v2, v3), etc...
>> >
>> >So far I get,
>> >> cor(v)
>> >Error in cor(v) : supply both 'x' and 'y' or a matrix-like 'x'
>> >
>> >> vvv <- cbind(v, v, v)
>> >> cor(vvv)
>> > v v v
>> >v 1 1 1
>> >v 1 1 1
>> >v 1 1 1
>> >
>> >
>> >I am calculating squared exponential kernel as seen here.
>> >http://mlg.eng.cam.ac.uk/duvenaud/cookbook/index.html
>> >
>> >Thanks a bunch,
>> >
>> >Mike
>> >
>> > [[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