Vectors, Loops, Rnorm and KS-Testing

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Vectors, Loops, Rnorm and KS-Testing

Nicoletta Sabov
Hi there,

I need a function, that calculates the mean of a desired amount of normally distributed numbers and repeats this process for a desired number of repetitions. The function should return only a single vector consisting solely of the calculated means. Also the user of this function must be able to specify the parameters of the normal distribution used while calculating the means. I need this function for the Kolmogorov Smirnov Test.
I am a total beginner, so far I only have come to this point:

kolmo <- function(x, mean, sd, rep){
 
  for(i in rep){
    cat(mean(rnorm(x, mean, sd)))
   
}}
This returns a simple number, but I do need a vector to be returned.
Thanks a lot in advance!



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vectors, Loops, Rnorm and KS-Testing

Ivan Calandra-4
Hi Nicoletta,

You need to read more introductory material.
One of the problems with your code is that you actually don't create any
value, let alone any vector; you just display it with cat().
Then you try to create each value of your vector with an explicit for
loop, but you don't need to; replicate() does the job in a compact code.

So if I understand correctly what you want to achieve, I would do this:
kolmo <- function(x, meanR=0, sdR=1, repet=1){
     out <- replicate(repet, mean(rnorm(x, meanR, sdR)))
     return(out)
}
# I changed the names of your arguments to make it less confusing (but I
wasn't imaginative)
# I set some default values for 'meanR' and 'sdR', which are actually
the defaults of rnorm()
# 'return(out)' is not necessary but I prefer to explicitly write what I
want the function to output

Example:
kolmo(x=10, repet=3)

HTH,
Ivan

--
Ivan Calandra, PhD
Scientific Mediator
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
[hidden email]
--
https://www.researchgate.net/profile/Ivan_Calandra
https://publons.com/author/705639/

Le 23/05/2016 à 11:30, Nicoletta Sabov a écrit :

> Hi there,
>
> I need a function, that calculates the mean of a desired amount of normally distributed numbers and repeats this process for a desired number of repetitions. The function should return only a single vector consisting solely of the calculated means. Also the user of this function must be able to specify the parameters of the normal distribution used while calculating the means. I need this function for the Kolmogorov Smirnov Test.
> I am a total beginner, so far I only have come to this point:
>
> kolmo <- function(x, mean, sd, rep){
>    
>    for(i in rep){
>      cat(mean(rnorm(x, mean, sd)))
>      
> }}
> This returns a simple number, but I do need a vector to be returned.
> Thanks a lot in advance!
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vectors, Loops, Rnorm and KS-Testing

Dylan Keenan
In reply to this post by Nicoletta Sabov
Use 'replicate.'


> replicate(n1, mean(rnorm(n2, mean, sd)))

Will compute return a column vector of length n1, each entry of which is
the mean of n2 random normal variables with mean and sd specified by the
arguments of rnorm.



On Mon, May 23, 2016 at 10:25 AM Nicoletta Sabov <[hidden email]>
wrote:

> Hi there,
>
> I need a function, that calculates the mean of a desired amount of
> normally distributed numbers and repeats this process for a desired number
> of repetitions. The function should return only a single vector consisting
> solely of the calculated means. Also the user of this function must be able
> to specify the parameters of the normal distribution used while calculating
> the means. I need this function for the Kolmogorov Smirnov Test.
> I am a total beginner, so far I only have come to this point:
>
> kolmo <- function(x, mean, sd, rep){
>
>   for(i in rep){
>     cat(mean(rnorm(x, mean, sd)))
>
> }}
> This returns a simple number, but I do need a vector to be returned.
> Thanks a lot in advance!
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.