[R] apply

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Fri Oct 4 15:20:18 CEST 2024


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 using r-project.org> On Behalf Of Ben Bolker
Sent: Friday, October 4, 2024 8:45 AM
To: r-help using 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 using 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 using 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 using 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 using 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.



More information about the R-help mailing list