• No se han encontrado resultados

‒ Euros/año

In document : BOMEH: PUBLICADO EN: (página 47-54)

To many people, it sounds like a contradiction in terms to generate random numbers on a computer since its results are supposed to be pre-dictable and reproducible. What is in fact possible is to generate sequences of “pseudo-random” numbers, which for practical purposes behave as if they were drawn randomly.

Here random numbers are used to give the reader a feeling for the way in which randomness affects the quantities that can be calculated from a set of data. In professional statistics, they are used to create simulated data sets in order to study the accuracy of mathematical approximations and the effect of assumptions being violated.

The use of the functions that generate random numbers is straightfor-ward. The first argument specifies the number of random numbers to compute, and the subsequent arguments are similar to those for other functions related to the same distributions. For instance,

> rnorm(10)

[1] -0.2996466 -0.1718510 -0.1955634 1.2280843 -2.6074190 [6] -0.2999453 -0.4655102 -1.5680666 1.2545876 -1.8028839

> rnorm(10)

[1] 1.7082495 0.1432875 -1.0271750 -0.9246647 0.6402383 [6] 0.7201677 -0.3071239 1.2090712 0.8699669 0.5882753

> rnorm(10,mean=7,sd=5)

[1] 8.934983 8.611855 4.675578 3.670129 4.223117 5.484290 [7] 12.141946 8.057541 -2.893164 13.590586

> rbinom(10,size=20,prob=.5) [1] 12 11 10 8 11 8 11 8 8 13

3.6 Exercises

3.1 Calculate the probability for each of the following events: (a) A standard normally distributed variable is larger than 3. (b) A normally distributed variable with mean 35 and standard deviation 6 is larger than 42. (c) Getting 10 out of 10 successes in a binomial distribution with prob-ability 0.8. (d) X<0.9 when X has the standard uniform distribution. (e) X>6.5 in a χ2distribution with 2 degrees of freedom.

3.2 A rule of thumb is that 5% of the normal distribution lies outside an interval approximately ±2s about the mean. To what extent is this true?

Where are the limits corresponding to 1%, 0.5%, and 0.1%? What is the position of the quartiles measured in standard deviation units?

3.3 For a disease known to have a postoperative complication frequency of 20%, a surgeon suggests a new procedure. He tests it on 10 patients and there are no complications. What is the probability of operating on 10 patients successfully with the traditional method?

3.4 Simulated coin-tossing can be done using rbinom instead of sample.

How exactly would you do that?

4

Descriptive statistics and graphics

Before going into the actual statistical modelling and analysis of a data set, it is often useful to make some simple characterizations of the data in terms of summary statistics and graphics.

4.1 Summary statistics for a single group

It is easy to calculate simple summary statistics with R. Here is how to calculate the mean, standard deviation, variance, and median.

> x <- rnorm(50)

> mean(x) [1] 0.03301363

> sd(x) [1] 1.069454

> var(x) [1] 1.143731

> median(x) [1] -0.08682795

Notice that the example starts with the generation of an artificial data vector x of 50 normally distributed observations. It is used in examples throughout this section. When reproducing the examples, you will not get exactly the same results since your random numbers will differ.

P. Dalgaard, Introductory Statistics with R,

DOI: 10.1007/978-0-387-79054-1_4, © Springer Science+Business Media, LLC 2008

Empirical quantiles may be obtained with the function quantile like this:

> quantile(x)

0% 25% 50% 75% 100%

-2.60741896 -0.54495849 -0.08682795 0.70018536 2.98872414

As you see, by default you get the minimum, the maximum, and the three quartiles — the 0.25, 0.50, and 0.75 quantiles — so named because they correspond to a division into four parts. Similarly, we have deciles for 0.1, 0.2, . . . , 0.9, and centiles or percentiles. The difference between the first and third quartiles is called the interquartile range (IQR) and is sometimes used as a robust alternative to the standard deviation.

It is also possible to obtain other quantiles; this is done by adding an argu-ment containing the desired percentage points. This, for example, is how to get the deciles:

-2.60741896 -1.07746896 -0.70409272 -0.46507213 -0.29976610

50% 60% 70% 80% 90%

-0.08682795 0.19436950 0.49060129 0.90165137 1.31873981 100%

2.98872414

Be aware that there are several possible definitions of empirical quantiles.

The one R uses by default is based on a sum polygon where the ith ranking observation is the(i − 1)/(n − 1)quantile and intermediate quantiles are obtained by linear interpolation. It sometimes confuses students that in a sample of 10 there will be 3 observations below the first quartile with this definition. Other definitions are available via the type argument to quantile.

If there are missing values in data, things become a bit more complicated.

For illustration, we use the following example.

The data set juul contains variables from an investigation performed by Anders Juul (Rigshospitalet, Department for Growth and Reproduction) concerning serum IGF-I (insulin-like growth factor) in a group of healthy humans, primarily schoolchildren. The data set is contained in the ISwR package and contains a number of variables, of which we only use igf1 (serum IGF-I) for now, but later in the chapter we also use tanner (Tan-ner stage of puberty, a classification into five groups based on appearance

4.1 Summary statistics for a single group 69 of primary and secondary sexual characteristics), sex, and menarche (indicating whether or not a girl has had her first period).

Attempting to calculate the mean of igf1 reveals a problem.

> attach(juul)

> mean(igf1) [1] NA

Rwill not skip missing values unless explicitly requested to do so. The mean of a vector with an unknown value is unknown. However, you can give the na.rm argument (not available, remove) to request that missing values be removed:

> mean(igf1,na.rm=T) [1] 340.168

There is one slightly annoying exception: The length function will not understand na.rm, so we cannot use it to count the number of nonmissing measurements of igf1. However, you can use

> sum(!is.na(igf1)) [1] 1018

The construction above uses the fact that if logical values are used in arithmetic, then TRUE is converted to 1 and FALSE to 0.

A nice summary display of a numeric variable is obtained from the summaryfunction:

> summary(igf1)

Min. 1st Qu. Median Mean 3rd Qu. Max. NA’s 25.0 202.2 313.5 340.2 462.8 915.0 321.0

The 1st Qu. and 3rd Qu. refer to the empirical quartiles (0.25 and 0.75 quantiles).

In fact, it is possible to summarize an entire data frame with

> summary(juul)

age menarche sex

Min. : 0.170 Min. : 1.000 Min. :1.000 1st Qu.: 9.053 1st Qu.: 1.000 1st Qu.:1.000 Median :12.560 Median : 1.000 Median :2.000 Mean :15.095 Mean : 1.476 Mean :1.534 3rd Qu.:16.855 3rd Qu.: 2.000 3rd Qu.:2.000 Max. :83.000 Max. : 2.000 Max. :2.000 NA’s : 5.000 NA’s :635.000 NA’s :5.000

igf1 tanner testvol

Min. : 25.0 Min. : 1.000 Min. : 1.000 1st Qu.:202.2 1st Qu.: 1.000 1st Qu.: 1.000

Median :313.5 Median : 2.000 Median : 3.000 Mean :340.2 Mean : 2.640 Mean : 7.896 3rd Qu.:462.8 3rd Qu.: 5.000 3rd Qu.: 15.000 Max. :915.0 Max. : 5.000 Max. : 30.000 NA’s :321.0 NA’s :240.000 NA’s :859.000

The data set has menarche, sex, and tanner coded as numeric variables even though they are clearly categorical. This can be mended as follows:

> detach(juul) 1st Qu.: 9.053 Yes :335 F :713 1st Qu.:202.2 Median :12.560 NA’s:635 NA’s: 5 Median :313.5

Mean :15.095 Mean :340.2

Notice how the display changes for the factor variables. Note also that juulwas detached and reattached after the modification. This is because modifying a data frame does not affect any attached version. It was not strictly necessary to do it here because summary works directly on the data frame whether attached or not.

In the above, the variables sex, menarche, and tanner were converted to factors with suitable level names (in the raw data these are represented using numeric codes). The converted variables were put back into the data frame juul, replacing the original sex, tanner, and menarche variables. We might also have used the transform function (or within):

> juul <- transform(juul,

+ sex=factor(sex,labels=c("M","F")),

+ menarche=factor(menarche,labels=c("No","Yes")),

+ tanner=factor(tanner,labels=c("I","II","III","IV","V")))

In document : BOMEH: PUBLICADO EN: (página 47-54)

Documento similar