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. > >[[alternative HTML version deleted]]
? 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