sadII_lab1_rozklady_dyskretne

2336 days ago by agorska

# Przelacz notatnik w tryb %r ^ library(lattice) 
       
%html Korzystamy m.in z <a href="http://biecek.pl/RinHasselt/manuals/RandStatisticsWithBiologicalExamples.pdf"> tego skryptu.</a> 
       
Korzystamy m.in z tego skryptu.
%python print "konik" 
       
konik
#Preczytaj o funkcjach dot, rozkladow w R ?dbinom #pois #nbinom #http://en.wikipedia.org/wiki/Negative_binomial_distribution #unif 
       
Binomial                 package:stats                 R
Documentation

_T_h_e _B_i_n_o_m_i_a_l
_D_i_s_t_r_i_b_u_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     Density, distribution function, quantile function and random
     generation for the binomial distribution with parameters 'size'
     and 'prob'.

_U_s_a_g_e:

     dbinom(x, size, prob, log = FALSE)
     pbinom(q, size, prob, lower.tail = TRUE, log.p = FALSE)
     qbinom(p, size, prob, lower.tail = TRUE, log.p = FALSE)
     rbinom(n, size, prob)
     
_A_r_g_u_m_e_n_t_s:

    x, q: vector of quantiles.

       p: vector of probabilities.

       n: number of observations. If 'length(n) > 1', the length
is
          taken to be the number required.

    size: number of trials (zero or more).

    prob: probability of success on each trial.

log, log.p: logical; if TRUE, probabilities p are given as log(p).

lower.tail: logical; if TRUE (default), probabilities are P[X <=
x],
          otherwise, P[X > x].

_D_e_t_a_i_l_s:

     The binomial distribution with 'size' = n and 'prob' = p has
     density

                                                                    
p(x) = choose(n,x) p^x (1-p)^(n-x)               
     
     for x = 0, \ldots, n.

     If an element of 'x' is not integer, the result of 'dbinom' is
     zero, with a warning.  p(x) is computed using Loader's
algorithm,
     see the reference below.

     The quantile is defined as the smallest value x such that F(x)
>=
     p, where F is the distribution function.

_V_a_l_u_e:

     'dbinom' gives the density, 'pbinom' gives the distribution
     function, 'qbinom' gives the quantile function and 'rbinom'
     generates random deviates.

     If 'size' is not an integer, 'NaN' is returned.

_S_o_u_r_c_e:

     For 'dbinom' a saddle-point expansion is used: see

     Catherine Loader (2000). _Fast and Accurate Computation of
     Binomial Probabilities_; available from <URL:
     http://www.herine.net/stat/software/dbinom.html>.

     'pbinom' uses 'pbeta'.

     'qbinom' uses the Cornish-Fisher Expansion to include a
skewness
     correction to a normal approximation, followed by a search.

     'rbinom' (for 'size < .Machine$integer.max') is based on

     Kachitvichyanukul, V. and Schmeiser, B. W. (1988) Binomial
random
     variate generation.  _Communications of the ACM_, *31*,
216-222.

_S_e_e _A_l_s_o:

     'dnbinom' for the negative binomial, and 'dpois' for the
Poisson
     distribution.

_E_x_a_m_p_l_e_s:

     require(graphics)
     # Compute P(45 < X < 55) for X Binomial(100,0.5)
     sum(dbinom(46:54, 100, 0.5))
     
     ## Using "log = TRUE" for an extended range :
     n <- 2000
     k <- seq(0, n, by = 20)
     plot (k, dbinom(k, n, pi/10, log=TRUE), type='l', ylab="log
density",
           main = "dbinom(*, log=TRUE) is better than 
log(dbinom(*))")
     lines(k, log(dbinom(k, n, pi/10)), col='red', lwd=2)
     ## extreme points are omitted since dbinom gives 0.
     mtext("dbinom(k, log=TRUE)", adj=0)
     mtext("extended range", adj=0, line = -1, font=4)
     mtext("log(dbinom(k))", col="red", adj=1)
     
histogram(rbinom(1000,10,0.5),breaks=11) #histogram(~ rbinom(1000,10,0.5),breaks=11) dev.off() ?histogram 
       
WARNING: Output truncated!  
full_output.txt





null device 
          1 
B_03_histogram             package:lattice             R
Documentation

_H_i_s_t_o_g_r_a_m_s _a_n_d _K_e_r_n_e_l
_D_e_n_s_i_t_y _P_l_o_t_s

_D_e_s_c_r_i_p_t_i_o_n:

     Draw Histograms and Kernel Density Plots, possibly conditioned
on
     other variables.

_U_s_a_g_e:

     histogram(x, data, ...)
     densityplot(x, data, ...)
     ## S3 method for class 'formula':
     histogram(x,
               data,
               allow.multiple, outer = TRUE,
               auto.key = FALSE,
               aspect = "fill",
               panel = lattice.getOption("panel.histogram"),
               prepanel, scales, strip, groups,
               xlab, xlim, ylab, ylim,
               type = c("percent", "count", "density"),
               nint = if (is.factor(x)) nlevels(x)
               else round(log2(length(x)) + 1),
               endpoints = extend.limits(range(as.numeric(x), finite
= TRUE), prop = 0.04),
               breaks,
               equal.widths = TRUE,
               drop.unused.levels =
lattice.getOption("drop.unused.levels"),
               ...,
               lattice.options = NULL,
               default.scales = list(),
               subscripts,
               subset)
     
     ## S3 method for class 'numeric':
     histogram(x, data = NULL, xlab, ...)
     ## S3 method for class 'factor':
     histogram(x, data = NULL, xlab, ...)
     
     
     ## S3 method for class 'formula':
     densityplot(x,
                 data,
                 allow.multiple = is.null(groups) || outer,
                 outer = !is.null(groups),
                 auto.key = FALSE,
                 aspect = "fill",
                 panel = lattice.getOption("panel.densityplot"),
                 prepanel, scales, strip, groups, weights,
                 xlab, xlim, ylab, ylim,
                 bw, adjust, kernel, window, width, give.Rkern,
                 n = 50, from, to, cut, na.rm,
                 drop.unused.levels =
lattice.getOption("drop.unused.levels"),
                 ...,

...

     call to 'densityplot' to control the output.  See documentation
of
     'density' for details. (Note: The default value of the argument
     'n' of 'density' is changed to 50.)

     These and all other high level Trellis functions have several
     arguments in common. These are extensively documented only in
the
     help page for 'xyplot', which should be consulted to learn more
     detailed usage.

     'do.breaks' is an utility function that calculates breakpoints
     given an interval and the number of pieces to break it into.

_V_a_l_u_e:

     An object of class '"trellis"'. The 'update' method can be used
to
     update components of the object and the 'print' method (usually
     called by default) will plot it on an appropriate plotting
device.

_N_o_t_e:

     The form of the arguments accepted by the default panel
function
     'panel.histogram' is different from that in S-PLUS. Whereas
S-PLUS
     calculates the heights inside 'histogram' and passes only the
     breakpoints and the heights to the panel function, here the
     original variable 'x' is passed along with the breakpoints.
This
     allows plots as in the second example below.

_A_u_t_h_o_r(_s):

     Deepayan Sarkar <email: Deepayan.Sarkar@R-project.org>

_R_e_f_e_r_e_n_c_e_s:

     Sarkar, Deepayan (2008) "Lattice: Multivariate Data
Visualization
     with R", Springer. <URL:
http://lmdvr.r-forge.r-project.org/>

_S_e_e _A_l_s_o:

     'xyplot', 'panel.histogram', 'density', 'panel.densityplot',
     'panel.mathdensity', 'Lattice'

_E_x_a_m_p_l_e_s:

     require(stats)
     histogram( ~ height | voice.part, data = singer, nint = 17,
               endpoints = c(59.5, 76.5), layout = c(2,4), aspect =
1,
               xlab = "Height (inches)")
     
     histogram( ~ height | voice.part, data = singer,
               xlab = "Height (inches)", type = "density",
               panel = function(x, ...) {
                   panel.histogram(x, ...)
                   panel.mathdensity(dmath = dnorm, col = "black",
                                     args =
list(mean=mean(x),sd=sd(x)))
               } )
     
     densityplot( ~ height | voice.part, data = singer, layout =
c(2, 4),  
                 xlab = "Height (inches)", bw = 5)
     
dist.data<-data.frame(p=seq(0,1,by=0.1)) png(width=1200,height=400) histogram( ~ rbinom(1000,10,p) | factor(p) , data=dist.data) densityplot( ~ rbinom(1000,10,p) | factor(p) , data=dist.data) dev.off() 
       
null device 
          1 

#Narysuj podobny wykres dla rozkladu Poissona oraz negative binomial dist.data<-data.frame(p=seq(0,10,by=1)) png(width=1200,height=400) histogram( ~ rpois(1000, p) | factor(p) , data=dist.data) histogram( ~ rpois(10000, p) | factor(p) , data=dist.data) paczki<-da histogram( ~ rgeom(10000, p) | factor(p) , data=dist.data) #densityplot( ~ rgeo(1000,10,p) | p , data=dist.data) dev.off() 
       
Error: object 'da' not found
Warning message:
In rgeom(10000, p) : NAs produced

null device 
          1 


dist.data<-data.frame(x=seq(-1,20,by=0.1)) png(width=1200,height=400) xyplot( dpois(x,1)~x , data=dist.data, type='b') xyplot( dpois(x,3)~x , data=dist.data, type='b') xyplot( dpois(x,5)~x , data=dist.data, type='b') dev.off() 
       
There were 50 or more warnings (use warnings() to see the first 50)
There were 50 or more warnings (use warnings() to see the first 50)
There were 50 or more warnings (use warnings() to see the first 50)
null device 
          1 


dist.data<-data.frame(quantiles=seq(0,1,by=0.1)) png(width=1200,height=400) xyplot( quantiles ~ qpois(quantiles,3) , data=dist.data, type='p') xyplot( quantiles ~ qpois(quantiles,5) , data=dist.data, type='p') dev.off() 
       
null device 
          1 

# Ile paczkow dzis zjedliscie? paczki<-data.frame(ile=c(0,0,1,1,1,2)) p <- mean(paczki) #histogram( ~ rpois(100000, mean(paczki))) #histogram( ~ rgeom(100000, mean(paczki))) dev.off() p #png(width=1200,height=400) dpois() 
       
Error in dev.off() : cannot shut down device 1 (the null device)
      ile 
0.8333333 

Error in dpois() : element 1 is empty;
   the part of the args list of '.Internal' being evaluated was:
   (x, lambda, log)
dpois(paczki$ile,0.1) 
       
[1] 0.904837418 0.904837418 0.090483742 0.090483742 0.090483742
0.004524187
#poisson lambda<-data.frame(p=seq(0,20,by=0.001)) pois.paczki <- function (lam){ Reduce("*", dpois(paczki$ile,lam))} geom.paczki <- function (lam){ Reduce("*", dgeom(paczki$ile,lam))} lambda$lik.pois<-sapply(lambda$p, pois.paczki) lambda$lik.geom<-sapply(1/lambda$p, geom.paczki) which.max(lambda$lik.pois) which.max(lambda$lik.geom) lambda$p[which.max(lambda$lik.pois)] lambda$p[which.max(lambda$lik.geom)] 
       
There were 50 or more warnings (use warnings() to see the first 50)
[1] 834
[1] 1834
[1] 0.833
[1] 1.833