[R] A simple simulation question

2012-07-12 Thread Aldous Huxley
Hi!

I would like to post the following question:

I was trying to figure out how to do the simulation shown in Fig 10.6 of
John Verzani's book 'Using R for Intro Statistics'. It is on page 290, with
a description on the previous page. It seems like a simple thing... Just
needing to duplicate a procedure. (Perhaps I need to do it with a loop?)

This is what I was trying:

replicate(100, {
abline(lm(y~x))
plot(y~x)
y=rnorm(100,x,5)
x=rep(1:10,10)
}

Obviously not correct. Can anyone help me with a simple way to do this?

With thanks,
Manning

[[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.


Re: [R] A simple simulation question

2012-07-12 Thread Ivan Calandra

Hi Manning,

There are two obvious mistakes:
- close the brackets for replicate() and
- do the things in the correct order

replicate(100, {
x=rep(1:10,10)   ## first define x
y=rnorm(100,x,5)  ## and then y because it depends on x
plot(y~x)  ## then plot, because it depends on x and y
abline(lm(y~x))  ## then add the line because it depends on plot()
})  ## don't forget the last bracket

Now it seems to work!
HTH,
Ivan

--
Ivan CALANDRA
Université de Bourgogne
UMR CNRS/uB 6282 Biogéosciences
6 Boulevard Gabriel
21000 Dijon, FRANCE
+33(0)3.80.39.63.06
ivan.calan...@u-bourgogne.fr
http://biogeosciences.u-bourgogne.fr/calandra

Le 12/07/12 14:55, Aldous Huxley a écrit :

Hi!

I would like to post the following question:

I was trying to figure out how to do the simulation shown in Fig 10.6 of
John Verzani's book 'Using R for Intro Statistics'. It is on page 290, with
a description on the previous page. It seems like a simple thing... Just
needing to duplicate a procedure. (Perhaps I need to do it with a loop?)

This is what I was trying:

replicate(100, {
abline(lm(y~x))
plot(y~x)
y=rnorm(100,x,5)
x=rep(1:10,10)
}

Obviously not correct. Can anyone help me with a simple way to do this?

With thanks,
Manning

__
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.


Re: [R] A simple simulation question

2012-07-12 Thread John Kane
I think that you have the statements out of order and I know that you are 
lacking the last )  to match the replicate(

Try this
replicate(100, {
x=rep(1:10,10)
y=rnorm(100,x,5)
plot(y~x)
abline(lm(y~x))
})

John Kane
Kingston ON Canada


 -Original Message-
 From: aldoushuxle...@gmail.com
 Sent: Thu, 12 Jul 2012 13:55:04 +0100
 To: r-help@r-project.org
 Subject: [R] A simple simulation question
 
 Hi!
 
 I would like to post the following question:
 
 I was trying to figure out how to do the simulation shown in Fig 10.6 of
 John Verzani's book 'Using R for Intro Statistics'. It is on page 290,
 with
 a description on the previous page. It seems like a simple thing... Just
 needing to duplicate a procedure. (Perhaps I need to do it with a loop?)
 
 This is what I was trying:
 
 replicate(100, {
 abline(lm(y~x))
 plot(y~x)
 y=rnorm(100,x,5)
 x=rep(1:10,10)
 }
 
 Obviously not correct. Can anyone help me with a simple way to do this?
 
 With thanks,
 Manning
 
   [[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.


FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks  orcas on your 
desktop!

__
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.