## 2767 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 . '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 _R_e_f_e_r_e_n_c_e_s: Sarkar, Deepayan (2008) "Lattice: Multivariate Data Visualization with R", Springer. _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) ``` full_output.txt
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)
 ``` 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)  834  1834  0.833  1.833```