Hello, I'm facing the following task: Simulate 500 values of the statistic (n-1)s^2/sigma^2 based on taking 500 samples of size n=40 from the N(mu=10; sigma^2 = 25) distribution. I think with the following command: rep(rnorm(40,10,25),times=500) I was able to simulate the samples but how I now can simulate the values of the chi-square-distribution is a mystery to me... I hope anyone can help me, shouldn't be a big problem... Thanks in advance! Reason for posting: I started a course at the University of Manchester and had to catch up with R, which I never used back in Switzerland. Now I'm solving some exercises to get more practice and this is one of them. -- View this message in context: http://r.789695.n4.nabble.com/Simulate-values-tp4705574.html Sent from the R help mailing list archive at Nabble.com.
People do not usually do homework stuff here but I do not see the point in repeating the same sample 500 times. You might want to simulate 40x500 independent samples and put those in a matrix (see ?matrix) with lets say 40 rows and 500 columns. Once done, you can apply calculate the 500 sample standard deviations (or the chi-square values) where the ?apply function might help you. Lets start with that and lets see where you get. kd ________________________________________ Felad?: R-help [r-help-bounces at r-project.org] ; meghatalmazó: Osiris10101 [osiris94 at gmx.ch] K?ldve: 2015. ?prilis 7. 0:52 To: r-help at r-project.org T?rgy: [R] Simulate values Hello, I'm facing the following task: Simulate 500 values of the statistic (n-1)s^2/sigma^2 based on taking 500 samples of size n=40 from the N(mu=10; sigma^2 = 25) distribution. I think with the following command: rep(rnorm(40,10,25),times=500) I was able to simulate the samples but how I now can simulate the values of the chi-square-distribution is a mystery to me... I hope anyone can help me, shouldn't be a big problem... Thanks in advance! Reason for posting: I started a course at the University of Manchester and had to catch up with R, which I never used back in Switzerland. Now I'm solving some exercises to get more practice and this is one of them. -- View this message in context: http://r.789695.n4.nabble.com/Simulate-values-tp4705574.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Thanks for your final paragraph, we sometimes see people who want us to do their homework for them and that does not go over well, but I think you situation is one where many would be happy to help. So here are some hints to help: The rnorm command expects the standard deviation, not the variance, so to generate normals with variance equal to 25 you need to specify the square root (5) as the 3rd argument. The rep command will just repeat the 40 random number 500 times, not generate new ones, so you will have the exact same sample (and all in one long vector). There are a few different ways to do this, but for easiest understanding I would suggest using the replicate function, this function will run a set of code the requested number of times, so think about how you would generate your data and compute the test statistic from the generated data, then pass that code to the replicate function (if it is more than 1 line then use curly brackets ({}) to group the lines of code) to generate your 500 test statistics. On Mon, Apr 6, 2015 at 4:52 PM, Osiris10101 <osiris94 at gmx.ch> wrote:> Hello, > > I'm facing the following task: > Simulate 500 values of the statistic (n-1)s^2/sigma^2 based on taking 500 > samples of size n=40 > from the N(mu=10; sigma^2 = 25) distribution. > > I think with the following command: > rep(rnorm(40,10,25),times=500) > I was able to simulate the samples but how I now can simulate the values of > the chi-square-distribution is a mystery to me... > > I hope anyone can help me, shouldn't be a big problem... > > Thanks in advance! > > Reason for posting: > I started a course at the University of Manchester and had to catch up with > R, which I never used back in Switzerland. Now I'm solving some exercises to > get more practice and this is one of them. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Simulate-values-tp4705574.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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 http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com
Hi Mike, please keep conversation on the list. Your code looks waaaay better now. I would not name the variable variances2 but that is just a matter of taste. What you could also do as a one-liner is something like n <- 40 REPS <- 50000 mu <- 10 sigma <- 5 chisq <- replicate(REPS,(n-1)*var(rnorm(n,mu,sigma))/sigma^2) Try to draw a histogram of the variances2 or chisq values and superimpose the theoratical distribution. ?hist and ?curve might help. Best, kd ________________________________ Felad?: "Mike Brechb?hler" [osiris94 at gmx.ch] K?ldve: 2015. ?prilis 8. 0:28 To: Kehl D?niel T?rgy: Aw: RE: [R] Simulate values Hey Daniel, Thanks for your answer. I changed my commands and ended up with: matrix <- matrix(rnorm(500 * 40, 10, 5), ncol = 40, nrow = 500) variances1 <- apply(matrix, 1, var) variances2 <-(40 - 1) * variances1 / 25 What do you think about it? Does that work and was more or less what you expected to see? Best wishes, Mike Gesendet: Dienstag, 07. April 2015 um 05:39 Uhr Von: "Kehl D?niel" <kehld at ktk.pte.hu> An: Osiris10101 <osiris94 at gmx.ch>, "r-help at r-project.org" <r-help at r-project.org> Betreff: RE: [R] Simulate values People do not usually do homework stuff here but I do not see the point in repeating the same sample 500 times. You might want to simulate 40x500 independent samples and put those in a matrix (see ?matrix) with lets say 40 rows and 500 columns. Once done, you can apply calculate the 500 sample standard deviations (or the chi-square values) where the ?apply function might help you. Lets start with that and lets see where you get. kd ________________________________________ Felad?: R-help [r-help-bounces at r-project.org] ; meghatalmazó: Osiris10101 [osiris94 at gmx.ch] K?ldve: 2015. ?prilis 7. 0:52 To: r-help at r-project.org T?rgy: [R] Simulate values Hello, I'm facing the following task: Simulate 500 values of the statistic (n-1)s^2/sigma^2 based on taking 500 samples of size n=40 from the N(mu=10; sigma^2 = 25) distribution. I think with the following command: rep(rnorm(40,10,25),times=500) I was able to simulate the samples but how I now can simulate the values of the chi-square-distribution is a mystery to me... I hope anyone can help me, shouldn't be a big problem... Thanks in advance! Reason for posting: I started a course at the University of Manchester and had to catch up with R, which I never used back in Switzerland. Now I'm solving some exercises to get more practice and this is one of them. -- View this message in context: http://r.789695.n4.nabble.com/Simulate-values-tp4705574.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]