Multiple plots
To print 4 histograms (with 100 classes) of random normal distribution samples on the same plot, where n=351, mean=160, sd=6.03:
> par(mfrow=c(2,2))
> for (i in 1:4){
hist(rnorm(351, 160, 6.03), 100)
}
Gives the following plot (yours may differ because of randomness):
To produce a jpeg:
> jpeg("~/multiplot.jpg")
> par(mfrow=c(3,3))
> for (i in 1:9){
hist(rnorm(351, 160, 6.03), 100)
}
> dev.off()
Which produces the following image:
Note: The dimensions of the image are 480x480 pixels, hence the titles being truncated. The following is more appropriate:
> jpeg("~/multiplot.jpg", 600, 600)
Cumalative plot
coal.txt contains the interval in days between successive mine explosions. The following command can be used to produce a cumulative plot:
> x<-scan("coal.txt") //read data
> time<-cumsum(x) //cumulative 'running' sum of the data
> number<-1:length(x) //sequence from 1:190
> plot(time, number) //giving:
> scatter.smooth(x=time, y=number) //or a scatter with smoothed curve overlay:
Poisson distribution
To plot a poisson probability function Poission(mu) from the data in
dec20.txt, issue the following commands:
> dec20<-scan("dec20.txt") //load in data
> barplot(dpois(1:max(dec20),summary(dec20)['Mean'])) //prints the following:
Note: the syntax to get the mean from the summary is not straight forward. Because
summary(...) returns an atomic vector, hence the
$ notation cannot be used to (de-)reference the internals of the returned data structure, like it can from say
hist(x,y)$counts. The array syntax
[] should be used, hence:
summary(dec20)['Mean'] to get hold of the mean/mu. You might find it enlightening to issue the following commands:
mode(summary(dec20)),
mode(hist(x)) to get a feel for the different data structures returned from various functions. You might also like to try
attributes(...).
Histograms
> hist(dec20) //plots the following
But can be improved with the following:
> hist(dec20, 23, right=F)
Notice that the second plot has more groups/classes, and the frequency for 0 is now plotted separately,
right=F.
Poisson process
earth.txt contains intervals in days between major earthquakes.
> e<-scan("earth.txt")
The following finds the probability that exactly 5 earthquakes happen in a typical 4 year period:
> dpois(5,((365*3)+366)/summary(e)['Mean'])
And the following finds the probability that fewer than 3 earthquakes happen in a 4 year period:
> ppois(2,((365*3)+366)/summary(e)['Mean'])
The waiting times between successive earthquakes can be described through an exponential distribution, with parameter lambda (rate, number of earthquakes a day).
The probability that the waiting time will be less than the 30 days:
> pexp(30, 62/27107)
and will exceed two years:
> 1-pexp(2*365, 62/27107)
Quantiles for an exponential distribution
The mean of the interval between serious earthquakes is 437 days (see above for data). The median can be calculated using P(X<= x)=0.5:
> qexp(.5, 1/437)
which results in approx 303 days. 1/437 is the rate of earthquakes (1/mu). Any quantile can be calculated in this manner.
to top