The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
apply
17 messages · Uwe Ligges, Ivan Krylov, Steven Yen +6 more
Homework questions are not answered on this list. Best, Uwe Ligges
On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Pardon me!!! What makes you think this is a homework question? You are not obligated to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable with the online do of apply.
set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 >
On 10/4/2024 4:57 PM, Uwe Ligges wrote:
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On 10/4/2024 5:13 PM, Steven Yen wrote:
Pardon me!!! What makes you think this is a homework question? You are not obligated to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable with the online documentation of "apply".
set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > On 10/4/2024 4:57 PM, Uwe Ligges wrote:
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On 04.10.2024 11:13, Steven Yen wrote:
Pardon me!!! What makes you think this is a homework question? You are not obligated
Otherwise you called cov() Best, Uwe Ligges
to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable with the online do of apply.
set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > On 10/4/2024 4:57 PM, Uwe Ligges wrote:
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hello, If you have a numeric matrix or data.frame, try something like cov(mtcars) Hope this helps, Rui Barradas ?s 10:15 de 04/10/2024, Steven Yen escreveu:
On 10/4/2024 5:13 PM, Steven Yen wrote:
Pardon me!!! What makes you think this is a homework question? You are not obligated to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable with the online documentation of "apply".
set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > On 10/4/2024 4:57 PM, Uwe Ligges wrote:
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
Hello I have a vector: set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 1.55870831 I like to create a matrix with elements containing variances and covariances of x. That is var(x[1]) cov(x[1],x[2]) cov(x[1],x[3]) cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) cov(x[3],x[2]) var(x[3]) And I like to do it with "apply". Thanks.
On 10/4/2024 6:35 PM, Rui Barradas wrote:
Hello, If you have a numeric matrix or data.frame, try something like cov(mtcars) Hope this helps, Rui Barradas ?s 10:15 de 04/10/2024, Steven Yen escreveu:
On 10/4/2024 5:13 PM, Steven Yen wrote:
Pardon me!!! What makes you think this is a homework question? You are not obligated to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable with the online documentation of "apply".
set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > On 10/4/2024 4:57 PM, Uwe Ligges wrote:
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
????[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
? Fri, 4 Oct 2024 19:14:30 +0800 Steven Yen <styen at ntu.edu.tw> ?????:
I have a vector:
set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 1.55870831
var(x[1]) cov(x[1],x[2])
Are you sure you don't have a matrix? If you type var(x[1]) or cov(x[1],x[2]) into R, you can see that all these are NA: an unbiased estimate of variance or covariance requires dividing by (sample size - 1), which would be 0 for individual numbers. Even if you did divide by (sample size), the answers would all be 0, because a single number is always equal to the mean of the same one number.
Best regards, Ivan
Hello, This doesn't make sense, if you have only one vector you can estimate its variance with var(x) but there is no covariance, the joint variance of two rv's. "co" or joint with what if you have only x? Note that the variance of x[1] or any other vector element is zero, it's only one value therefore it does not vary. A similar reasonong can be applied to cov(x[1], x[2]), etc. Hope this helps, Rui Barradas ?s 12:14 de 04/10/2024, Steven Yen escreveu:
Hello I have a vector: set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 1.55870831 I like to create a matrix with elements containing variances and covariances of x. That is var(x[1]) cov(x[1],x[2]) cov(x[1],x[3]) cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) cov(x[3],x[2]) var(x[3]) And I like to do it with "apply". Thanks. On 10/4/2024 6:35 PM, Rui Barradas wrote:
Hello, If you have a numeric matrix or data.frame, try something like cov(mtcars) Hope this helps, Rui Barradas ?s 10:15 de 04/10/2024, Steven Yen escreveu:
On 10/4/2024 5:13 PM, Steven Yen wrote:
Pardon me!!! What makes you think this is a homework question? You are not obligated to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable with the online documentation of "apply".
set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > On 10/4/2024 4:57 PM, Uwe Ligges wrote:
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
????[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
OK. Thanks to all. Suppose I have two vectors, x and y. Is there a way to do the covariance matrix with ?apply?. The matrix I need really contains the deviation products divided by the degrees of freedom (n-1). That is, the elements (1,1), (1,2),...,(1,n) (2,1), (2,2),...., (2,n) .... (n,1),(n,2),...,(n,n).
Hello, This doesn't make sense, if you have only one vector you can estimate its variance with var(x) but there is no covariance, the joint variance of two rv's. "co" or joint with what if you have only x? Note that the variance of x[1] or any other vector element is zero, it's only one value therefore it does not vary. A similar reasonong can be applied to cov(x[1], x[2]), etc. Hope this helps, Rui Barradas ?s 12:14 de 04/10/2024, Steven Yen escreveu:
Hello I have a vector: set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 1.55870831 I like to create a matrix with elements containing variances and covariances of x. That is var(x[1]) cov(x[1],x[2]) cov(x[1],x[3]) cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) cov(x[3],x[2]) var(x[3]) And I like to do it with "apply". Thanks. On 10/4/2024 6:35 PM, Rui Barradas wrote:
Hello, If you have a numeric matrix or data.frame, try something like cov(mtcars) Hope this helps, Rui Barradas ?s 10:15 de 04/10/2024, Steven Yen escreveu:
On 10/4/2024 5:13 PM, Steven Yen wrote:
Pardon me!!! What makes you think this is a homework question? You are not obligated to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable with the online documentation of "apply".
set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > On 10/4/2024 4:57 PM, Uwe Ligges wrote:
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
????[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
? Fri, 4 Oct 2024 20:28:01 +0800 Steven Yen <styen at ntu.edu.tw> ?????:
Suppose I have two vectors, x and y. Is there a way to do the covariance matrix with ?apply?.
There is no covariance matrix for just two samples (vectors) 'x' and 'y'. You can only get one covariance value for these. If you had a pair of vectors of _random variates_, the situation would be different, but those are more abstract mathematical concepts. You would need to sample every random variate, producing two matrices 'x' and 'y' in order to calculate a covariance matrix for them.
Best regards, Ivan
It's still hard to figure out what you want. If you have two vectors you can compute their (2x2) covariance matrix using cov(cbind(x,y)). If you want to compute all pairwise squared differences between elements of x and y you could use outer(x, y, "-")^2. Can you explain a little bit more about (1) the context for your question and (2) why you want/need to use apply() ?
On 2024-10-04 8:28 a.m., Steven Yen wrote:
OK. Thanks to all. Suppose I have two vectors, x and y. Is there a way to do the covariance matrix with ?apply?. The matrix I need really contains the deviation products divided by the degrees of freedom (n-1). That is, the elements (1,1), (1,2),...,(1,n) (2,1), (2,2),...., (2,n) .... (n,1),(n,2),...,(n,n).
Hello, This doesn't make sense, if you have only one vector you can estimate its variance with var(x) but there is no covariance, the joint variance of two rv's. "co" or joint with what if you have only x? Note that the variance of x[1] or any other vector element is zero, it's only one value therefore it does not vary. A similar reasonong can be applied to cov(x[1], x[2]), etc. Hope this helps, Rui Barradas ?s 12:14 de 04/10/2024, Steven Yen escreveu:
Hello I have a vector: set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 1.55870831 I like to create a matrix with elements containing variances and covariances of x. That is var(x[1]) cov(x[1],x[2]) cov(x[1],x[3]) cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) cov(x[3],x[2]) var(x[3]) And I like to do it with "apply". Thanks. On 10/4/2024 6:35 PM, Rui Barradas wrote:
Hello, If you have a numeric matrix or data.frame, try something like cov(mtcars) Hope this helps, Rui Barradas ?s 10:15 de 04/10/2024, Steven Yen escreveu:
On 10/4/2024 5:13 PM, Steven Yen wrote:
Pardon me!!! What makes you think this is a homework question? You are not obligated to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable with the online documentation of "apply".
set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > On 10/4/2024 4:57 PM, Uwe Ligges wrote:
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
????[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
Dr. Benjamin Bolker Professor, Mathematics & Statistics and Biology, McMaster University Director, School of Computational Science and Engineering > E-mail is sent at my convenience; I don't expect replies outside of working hours.
Why must the answer use apply? It feels like there are elements of the problem that are not explained. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Ben Bolker Sent: Friday, October 4, 2024 8:45 AM To: r-help at r-project.org Subject: Re: [R] apply [External Email] It's still hard to figure out what you want. If you have two vectors you can compute their (2x2) covariance matrix using cov(cbind(x,y)). If you want to compute all pairwise squared differences between elements of x and y you could use outer(x, y, "-")^2. Can you explain a little bit more about (1) the context for your question and (2) why you want/need to use apply() ?
On 2024-10-04 8:28 a.m., Steven Yen wrote:
OK. Thanks to all. Suppose I have two vectors, x and y. Is there a way to do the covariance matrix with "apply". The matrix I need really contains the deviation products divided by the degrees of freedom (n-1). That is, the elements (1,1), (1,2),...,(1,n) (2,1), (2,2),...., (2,n) .... (n,1),(n,2),...,(n,n).
Hello, This doesn't make sense, if you have only one vector you can estimate its variance with var(x) but there is no covariance, the joint variance of two rv's. "co" or joint with what if you have only x? Note that the variance of x[1] or any other vector element is zero, it's only one value therefore it does not vary. A similar reasonong can be applied to cov(x[1], x[2]), etc. Hope this helps, Rui Barradas ?s 12:14 de 04/10/2024, Steven Yen escreveu:
Hello I have a vector: set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 1.55870831 I like to create a matrix with elements containing variances and covariances of x. That is var(x[1]) cov(x[1],x[2]) cov(x[1],x[3]) cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) cov(x[3],x[2]) var(x[3]) And I like to do it with "apply". Thanks. On 10/4/2024 6:35 PM, Rui Barradas wrote:
Hello, If you have a numeric matrix or data.frame, try something like cov(mtcars) Hope this helps, Rui Barradas ?s 10:15 de 04/10/2024, Steven Yen escreveu:
On 10/4/2024 5:13 PM, Steven Yen wrote:
Pardon me!!! What makes you think this is a homework question? You are not obligated to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable with the online documentation of "apply".
set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > On 10/4/2024 4:57 PM, Uwe Ligges wrote:
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F %2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C02%7Cte bert%40ufl.edu%7Cbd77351d05ca4ffd998308dce4728e54%7C0d4da0f84a3 14d76ace60a62331e1b84%7C0%7C0%7C638636427922120864%7CUnknown%7C TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWw iLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=9mHV2ck%2FlVQlaA457k3M8b7etH %2BdlfHH1XXPvWcbaL4%3D&reserved=0 PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F %2Fwww.r-project.org%2Fposting-guide.html&data=05%7C02%7Ctebert %40ufl.edu%7Cbd77351d05ca4ffd998308dce4728e54%7C0d4da0f84a314d7 6ace60a62331e1b84%7C0%7C0%7C638636427922139431%7CUnknown%7CTWFp bGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJ XVCI6Mn0%3D%7C0%7C%7C%7C&sdata=TsIfyZl9gYLrlfoowyjLIz7jRZg65EXd V4bIr34pBVA%3D&reserved=0 and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2F stat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C02%7Ctebert%4 0ufl.edu%7Cbd77351d05ca4ffd998308dce4728e54%7C0d4da0f84a314d76ace6 0a62331e1b84%7C0%7C0%7C638636427922154301%7CUnknown%7CTWFpbGZsb3d8 eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D %7C0%7C%7C%7C&sdata=LcWqq5N8YO4tX7hPVTJOZl1nG6GC3Unq%2F46xSXspZjw% 3D&reserved=0 PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2F http://www.r-project.org/%2Fposting-&data=05%7C02%7Ctebert%40ufl.edu%7Cbd7 7351d05ca4ffd998308dce4728e54%7C0d4da0f84a314d76ace60a62331e1b84%7 C0%7C0%7C638636427922166033%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLj AwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C& sdata=VJclmilR7Wpywl7b%2BN1KSoFP6nYDTYG9%2B77EFiShKLg%3D&reserved= 0 guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat/ .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C02%7Ctebert%40ufl.edu %7Cbd77351d05ca4ffd998308dce4728e54%7C0d4da0f84a314d76ace60a62331e1b84 %7C0%7C0%7C638636427922176572%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata= RPev0nDdNEGZVUrut2P%2BSVtYWxbWX5MQ2H6rxiphsng%3D&reserved=0 PLEASE do read the posting guide https://www/. r-project.org%2Fposting-&data=05%7C02%7Ctebert%40ufl.edu%7Cbd77351d05c a4ffd998308dce4728e54%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638 636427922187632%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2 luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=idsohi4eloemUO oZ8uBXr5rujqgVN%2Fr4dQ2QIRaY4kQ%3D&reserved=0 guide.html and provide commented, minimal, self-contained, reproducible code.
-- Dr. Benjamin Bolker Professor, Mathematics & Statistics and Biology, McMaster University Director, School of Computational Science and Engineering > E-mail is sent at my convenience; I don't expect replies outside of working hours. ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hello, You don't need apply, covariance calculations are so frequent that R or any other statistics package already has pre-programmed functions. This time with two vectors x and y. set.seed(123) n <- 3 x <- rnorm(n) y <- rnorm(n) # the two main diagonal values var(x) #> [1] 1.300025 var(y) #> [1] 0.8704518 # the secondary diagonal values cov(x, y) #> [1] 1.056885 # cov(x, y) == cov(y, x) cov(y, x) #> [1] 1.056885 # and the result you are after # (the covariance matrix is symmetric) cov(cbind(x, y)) #> x y #> x 1.300025 1.0568845 #> y 1.056885 0.8704518 Hope this helps, Rui Barradas ?s 13:28 de 04/10/2024, Steven Yen escreveu:
OK. Thanks to all. Suppose I have two vectors, x and y. Is there a way to do the covariance matrix with ?apply?. The matrix I need really contains the deviation products divided by the degrees of freedom (n-1). That is, the elements (1,1), (1,2),...,(1,n) (2,1), (2,2),...., (2,n) .... (n,1),(n,2),...,(n,n).
Hello, This doesn't make sense, if you have only one vector you can estimate its variance with var(x) but there is no covariance, the joint variance of two rv's. "co" or joint with what if you have only x? Note that the variance of x[1] or any other vector element is zero, it's only one value therefore it does not vary. A similar reasonong can be applied to cov(x[1], x[2]), etc. Hope this helps, Rui Barradas ?s 12:14 de 04/10/2024, Steven Yen escreveu:
Hello I have a vector: set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 1.55870831 I like to create a matrix with elements containing variances and covariances of x. That is var(x[1]) cov(x[1],x[2]) cov(x[1],x[3]) cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) cov(x[3],x[2]) var(x[3]) And I like to do it with "apply". Thanks. On 10/4/2024 6:35 PM, Rui Barradas wrote:
Hello, If you have a numeric matrix or data.frame, try something like cov(mtcars) Hope this helps, Rui Barradas ?s 10:15 de 04/10/2024, Steven Yen escreveu:
On 10/4/2024 5:13 PM, Steven Yen wrote:
Pardon me!!! What makes you think this is a homework question? You are not obligated to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable with the online documentation of "apply".
set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > On 10/4/2024 4:57 PM, Uwe Ligges wrote:
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
????[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
Even if this is not a homework question, it smells like one. If you read the Posting Guide it warns you that homework is off-topic, so when you impose an arbitrary constraint like "must use specific unrelated function" we feel like you are either cheating or wasting our time, and it is up to you to explain why we should follow you down this rabbit hole, keeping in mind that statistics theory per-se is also off-topic here. You have yet to explain why you want to do this the hard way.
On October 4, 2024 2:13:26 AM PDT, Steven Yen <syen04 at gmail.com> wrote:
Pardon me!!! What makes you think this is a homework question? You are not obligated to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable with the online do of apply.
set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > On 10/4/2024 4:57 PM, Uwe Ligges wrote:
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Sent from my phone. Please excuse my brevity.
On Fri, 04 Oct 2024 11:16:45 -0700
Jeff Newmiller via R-help <r-help at r-project.org> wrote:
Even if this is not a homework question, it smells like one. If you read the Posting Guide it warns you that homework is off-topic, so when you impose an arbitrary constraint like "must use specific unrelated function" we feel like you are either cheating or wasting our time, and it is up to you to explain why we should follow you down this rabbit hole, keeping in mind that statistics theory per-se is also off-topic here. You have yet to explain why you want to do this the hard way.
Well put, Jeff. cheers, Rolf
On October 4, 2024 2:13:26 AM PDT, Steven Yen <syen04 at gmail.com> wrote:
Pardon me!!! What makes you think this is a homework question? You are not obligated to respond if the question is not intelligent enough for you. I did the following: two ways to calculate a covariance matrix but wonder how I might replicate the results with "apply". I am not too comfortable with the online do of apply.
set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 0.0151896601 > On 10/4/2024 4:57 PM, Uwe Ligges wrote:
Homework questions are not answered on this list. Best, Uwe Ligges On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column vector: se<-apply(dd,1,sd) How can I calculate the covariance matrix using apply? Thanks.
x <- runif(10) y <- runif(10) cov(cbind(x,y))
x y x 0.1205034 0.02642830 y 0.0264283 0.09945432 I understand wanting to calculate covariance matrices. What I DON'T understand is wanting to do it using apply(). (And that's what looked like a homework problem, it's so artificial.) On Sat, 5 Oct 2024 at 01:41, Ivan Krylov via R-help
<r-help at r-project.org> wrote:
? Fri, 4 Oct 2024 20:28:01 +0800 Steven Yen <styen at ntu.edu.tw> ?????:
Suppose I have two vectors, x and y. Is there a way to do the covariance matrix with ?apply?.
There is no covariance matrix for just two samples (vectors) 'x' and 'y'. You can only get one covariance value for these. If you had a pair of vectors of _random variates_, the situation would be different, but those are more abstract mathematical concepts. You would need to sample every random variate, producing two matrices 'x' and 'y' in order to calculate a covariance matrix for them. -- Best regards, Ivan
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.