Skip to content

apply

17 messages · Uwe Ligges, Ivan Krylov, Steven Yen +6 more

#
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.
#
Homework questions are not answered on this list.

Best,
Uwe Ligges
On 04.10.2024 10:32, 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 do of apply.
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:
#
On 10/4/2024 5:13 PM, Steven Yen wrote:

            
#
On 04.10.2024 11:13, Steven Yen wrote:
Otherwise you called cov()

Best,
Uwe Ligges
#
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:

  
    
#
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:
#
? Fri, 4 Oct 2024 19:14:30 +0800
Steven Yen <styen at ntu.edu.tw> ?????:
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.
#
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:

  
    
#
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).
#
? Fri, 4 Oct 2024 20:28:01 +0800
Steven Yen <styen at ntu.edu.tw> ?????:
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.
#
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:

  
    
#
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:
--
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:

  
    
#
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:

  
    
#
On Fri, 04 Oct 2024 11:16:45 -0700
Jeff Newmiller via R-help <r-help at r-project.org> wrote:

            
Well put, Jeff.

cheers,

Rolf
#
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: