Iara Faria
2011-Oct-27 17:44 UTC
[R] creating vector os zeros for simulations (beginner's question)
Dear R helpers, I know this is a simple task, but I'm new to R and I'm still havind difficulties with the language. I want to create 30 vectors to be used in a simulation, each with 1 columm and 5 lines, of random numbers N(0,1). What I tried was this: N=150 u2<-rep(1:150,0) u2<-list(matrix(0,5)) u2 for(i in 1:N) { u2[i]<-rnorm(5) } u2 ### also tried this: N=150 u2<-rep((matrix(0,5)),30) u2 for(i in 1:N) { u2[i]<-rnorm(5) } u2 The problem is none of this gives me the random numbers arranged in vectors. I've tried other variations too, but haven't managed to get it through. Any help is very welcome. Kind regards, Iara [[alternative HTML version deleted]]
Seb
2011-Oct-27 18:33 UTC
[R] creating vector os zeros for simulations (beginner's question)
you could try this: u2=matrix(rnorm(150), nrow=5, ncol=30); u2 Seb On Thu, Oct 27, 2011 at 1:44 PM, Iara Faria <iaragohn at yahoo.com.br> wrote:> Dear R helpers, > > I know this is a simple task, but I'm new to R and I'm still havind difficulties with the language. > I want to create 30 vectors to be used in a simulation, each?with 1 columm and 5 lines, of random numbers N(0,1). > What I tried was this: > > N=150 > u2<-rep(1:150,0) > u2<-list(matrix(0,5)) > u2 > for(i in 1:N) > { > u2[i]<-rnorm(5) > } > u2 > > ### also tried this: > > N=150 > u2<-rep((matrix(0,5)),30) > u2 > for(i in 1:N) > { > u2[i]<-rnorm(5) > } > u2 > > > The problem is none of this gives me the random numbers arranged in vectors. > I've tried other variations too, but haven't managed to get it through. > > Any help is very welcome. > Kind regards, > Iara > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >
Iara Faria
2011-Oct-28 12:26 UTC
[R] creating vector os zeros for simulations (beginner's question)
Thank you all very much, specially Ellison. I have been looking at some codes for simulation and I am trying to recreate them to my needs, but I understand I'm probably not doing it in the most efficient way. What you said was very useful. I really want to create a series of vector of random numbers, because later I will multiply them to a matrix P (choleski decomposition of variance-covariance of the residuals of a VAR), so to create a serie of vectors of shocks that will be added to my predictions. The thing is, I wasn't able to construct the vectors of random numbers in a way they could be multiplied by the matrix P . Would you be so kind as to point again what is wrong? I am finding it very difficult to get it right in terms of what the object must be to be used the way I want, a list, a data.frame, matrix, etc What I want to do is this: 1 - create a serie of N random vectors (actually, each 5 colums and 1 row), in this exercise N=30 2 - Create the serie P, by multiplying each of the random vectors by the same matrix c (choleski decomposition), which is 5x5, to get the shocks that consider the correlation between the variables. 3 - Create serie prev, with the predictions one step ahead of the variables, by multiplying the matrix coef (coefficients of the VAR) to the last prediction and adding the vector P of shocks. What I tried was: # 1- different ways of creating the random vectors, to see if any multiply by c: N=30 # N = numero de periodos para computar a trajetoria da divida u2<-list() for(i in 1:N) u2[[i]] <- rnorm(5) u2 str(u2) u3 <- as.data.frame(matrix(rnorm(150), nrow=N, ncol=5)) u3 str(u3) u4<-list() for (i in 1:N) { u4[[i]]<- matrix(rnorm(150), ncol=5, nrow=N) } u4 # 2- create serie P, of shocks ajusted by the correlations P2<-matrix(0, ncol=N, nrow=5) P2 for(i in 1:N){ P2[i]<-c%*%u2[i] } #but this is what i get: Error in c %*% u2[i] : requires numeric/complex matrix/vector arguments # i've tried taking out the [i], that doesn't work either. # tried also u3, u4.... # obs- c is matrix 5x5:> crp igpm ereal crescpib jurosreal rp 0.2249159 -0.02046234 0.004685928 -0.0002799183 2.639328e-04 igpm 0.0000000 0.59823056 -0.577864072 0.0070967189 -5.797647e-03 ereal 0.0000000 0.00000000 0.129118968 0.0007949951 -1.061969e-04 crescpib 0.0000000 0.00000000 0.000000000 0.0236250353 2.646392e-05 jurosreal 0.0000000 0.00000000 0.000000000 0.0000000000 2.027526e-03 # 3 - create serie previ2, size=N, # obs - prev2[1] = vector z, of last observations prev2<-c(z,rep(0,100)) prev2 for (i in 2:N) { prev2[i]<-coef%*%prev2[i-1]+P2[i] } Once again, thank you very much for the kind help. Regards, Iara ________________________________ De: S Ellison <S.Ellison@LGCGroup.com> Enviadas: Sexta-feira, 28 de Outubro de 2011 8:02 Assunto: RE: [R] creating vector os zeros for simulations (beginner's question)> -----Original Message----- > On Thu, Oct 27, 2011 at 1:44 PM, Iara Faria> > Dear R helpers, > > > > I know this is a simple task, but I'm new to R and I'm > still havind difficulties with the language. > > I want to create 30 vectors to be used in a simulation, > each with 1 columm and 5 lines, of random numbers N(0,1). > > What I tried was this: > > > > N=150 > > u2<-rep(1:150,0) > > u2<-list(matrix(0,5)) > > u2 > > for(i in 1:N) > > { > > u2[i]<-rnorm(5) > > } > > u2This line> > u2[i]<-rnorm(5)should be written u2[[i]]<-rnorm(5) u2[i] is a list element and ur[[i]] is the contents of the list element. Not quite the same thing. Your code could also be a great deal simpler as you don't need to generate a zero-filled list to start with. Try u2<-list() for(i in 1:N) u2[[i]] <- rnorm(5) But it's easier to generate the matrix version as Seb said. If you _really_ want a list - which can be useful, but is often unnecessary if you use apply() - you can also do u2 <- as.data.frame(matrix(rnorm(150), nrow=5, ncol=30)) which, since a data frame is a list, is also a list. And while we're on the topic, look at lapply and other versions of apply be[[elided Yahoo spam]]> > ### also tried this: > > > > N=150 > > u2<-rep((matrix(0,5)),30) > > u2 > > for(i in 1:N) > > { > > u2[i]<-rnorm(5) > > } > > u2 > > > > > > The problem is none of this gives me the random numbers > arranged in vectors. > > I've tried other variations too, but haven't managed to get > it through. > > > > Any help is very welcome. > > Kind regards, > > Iara > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > *******************************************************************This email and any attachments are confidential. Any use...{{dropped:10}}
Seemingly Similar Threads
- help with means using tail()
- how to use VARselect with missing values - beginner's question
- Fortran
- Beginner's question about plotting variables in a time series object with the date on the x axis
- Vector extracted from a matrix. How can I specify dimensions in as.matrix?