[R] Random number
David Forrest
drf5n at maplepark.com
Wed Oct 27 21:16:17 CEST 2004
On Wed, 27 Oct 2004, Kunal Shetty wrote:
> thank you Sundar,Andy, Partha for your prompt reply.
>
> as for the "%*%" is matrix multiplication problem sunder here is a sample of what i want...
>
> x <- rnorm(100,17,24) # X values
> y <- rnorm(100,7,11) # Y values
>
> # since X and Y values are independent to get their
> # covariance need to multiply each of them with a Matrix say A
>
> A <- matrix(c(10,25,8,40),nrow=2,ncol=2)
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)
>
> further i would like to assign both x and y vectors to one variale
>
> say z <- c(x,y)
>
> but if i do this my matrix multiplication fail
>
> i.e w <- A%*%z
> Error in A %*% z : non-conformable arguments ??
>
>
> so it tired multiply the X and Y vector individually
>
> x<- (A%*%x)
> y<- (A%*%y)
>
>
> Error in A %*% z : non-conformable arguments ??
>
>
> the error persist...sunder...
>
> or anybody who could direct me..?
>
> regards
> Kunal
>
>
>
>
>
> Sundar Dorai-Raj <sundar.dorai-raj at PDF.COM> wrote:
> >
> >
> > Kunal Shetty wrote:
> >
> > > Dear R- User
> > >
> > > I created two random number sets using rnorm function and calcuted their respective means. However now I would like to randomly
> > > replace some values with NA. Thus create my own test data.
> > > Any help or suggestions on this ?
> > >
> > >
> >
> > Use ?sample:
> > n <- 100
> > x <- rnorm(n)
> > x[sample(n, 4)] <- NA
> > sum(is.na(x))
> > # [1] 4
> >
> > > Also wanted to confirm when multiplying two random number vectors x am y by matrix..is this how i do it.
> > > A is the matrix
> > >
> > > z <- c(x,y) # x and y the two set of vectors
> > >
> > > w <- A%*%Z # each element in each vector multipled by the matrix .
> > >
> >
> > (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
> >
> > >
> > > regards
> > > Kunal
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
--
Dave Forrest
drf at vims.edu (804)684-7900w
drf5n at maplepark.com (804)642-0662h
http://maplepark.com/~drf5n/
More information about the R-help
mailing list