Skip to content

Random number

5 messages · Kunal Shetty, Sundar Dorai-Raj, David Forrest

#
Kunal Shetty wrote:

            
Use ?sample:
n <- 100
x <- rnorm(n)
x[sample(n, 4)] <- NA
sum(is.na(x))
# [1] 4
(Be careful: R is case sensitive (z != Z).)

I'm not really clear what you want here. "%*%" is matrix multiplication 
and "*" is elementwise multiplication. Also using "c" makes a vector or 
length "n = length(x) + length(y)". This implies that "A" above "p x n" 
(i.e. p rows, n columns) and the result "w" would be "p x 1". Please 
provide an example of what you expect to see by the multiplication.

--sundar
#
Kunal Shetty wrote:

            
Of course this doesn't work. `A' is 2x2, `x' and `y' are 100x1, and `z' 
is 200x1 (I think I even mentioned this previously). I *think* you want 
something like:

z <- rbind(x, y) # now 2 x 100

w <- A %*% z # 2 x 100

If not, please provide an example of what you expect, possibly worked by 
hand.

HTH,

--sundar
#
On Wed, 27 Oct 2004, Kunal Shetty wrote:

            
If x and y are independent, then their covariance should be
zero, or at least symmetric.

Are you looking for z as a bivariate normal with covariance matrix A and
means (17,7)?

 randMV<-function(n=NA,vMean=NA,mSig=NA){
 # create n observations of multivariate normal data with a
 # mean of vMean and covariance mSig
 #
  p<-length(vMean)
  VD<-eigen(mSig)
  mSigH<-VD$vectors%*%diag(sqrt(VD$values))%*%solve(VD$vectors)

  ret<-matrix(rnorm(n=p*n),ncol=p)%*%mSigH +rep(vMean,each=n)
  ret
 }

z<-randMV(100,c(17,7),A)

colMeans(z)
cov(z)