# r_lab_rozw1

## 3326 days ago by macieksk

%r faircoin <- function(nf_vec){ #Lamiemy wektor "na pol" twocol<-matrix(nf_vec,ncol=2) if (length(nf_vec)%%2 != 0) twocol <- twocol[-nrow(twocol),] #zabieramy ostatni wiersz #twocol # [...] - tutaj wpisz brakujacy kod porownujacy twocol[,1] i twocol[,2] - 1sza i druga kolumne macierzy #Mozna tak jak ponizej #res<-c() #for (i in 1:nrow(twocol)){ # if (twocol[i,1]!=twocol[i,2]) # res<-c(res,twocol[i,1]) #} #res #Ale mozna tez tak, i tak jest duzo szybciej - nie ma przyrostowej alokacji i petli goodpairs <- twocol[,1]!=twocol[,2] res<-twocol[goodpairs,1] res }
%r res<-faircoin(rnorm(2500)>1.0) summary(res)
 ``` Mode FALSE TRUE NA's logical 157 177 0 ```
%r #sum(res) - przy sumowaniu True zamieniane są na 1 a False na 0. binom.test(sum(res),length(res))
 ``` Exact binomial test data: sum(res) and length(res) number of successes = 177, number of trials = 334, p-value = 0.2985 alternative hypothesis: true probability of success is not equal to 0.5 95 percent confidence interval: 0.4748554 0.5844893 sample estimates: probability of success 0.5299401 ```
%r ?binom.test
 ```binom.test package:stats R Documentation _E_x_a_c_t _B_i_n_o_m_i_a_l _T_e_s_t _D_e_s_c_r_i_p_t_i_o_n: Performs an exact test of a simple null hypothesis about the probability of success in a Bernoulli experiment. _U_s_a_g_e: binom.test(x, n, p = 0.5, alternative = c("two.sided", "less", "greater"), conf.level = 0.95) _A_r_g_u_m_e_n_t_s: x: number of successes, or a vector of length 2 giving the numbers of successes and failures, respectively. n: number of trials; ignored if 'x' has length 2. p: hypothesized probability of success. alternative: indicates the alternative hypothesis and must be one of '"two.sided"', '"greater"' or '"less"'. You can specify just the initial letter. conf.level: confidence level for the returned confidence interval. _D_e_t_a_i_l_s: Confidence intervals are obtained by a procedure first given in Clopper and Pearson (1934). This guarantees that the confidence level is at least 'conf.level', but in general does not give the shortest-length confidence intervals. _V_a_l_u_e: A list with class '"htest"' containing the following components: statistic: the number of successes. parameter: the number of trials. p.value: the p-value of the test. conf.int: a confidence interval for the probability of success. estimate: the estimated probability of success. null.value: the probability of success under the null, 'p'. alternative: a character string describing the alternative hypothesis. method: the character string '"Exact binomial test"'. data.name: a character string giving the names of the data. _R_e_f_e_r_e_n_c_e_s: Clopper, C. J. & Pearson, E. S. (1934). The use of confidence or fiducial limits illustrated in the case of the binomial. _Biometrika_, *26*, 404-413. William J. Conover (1971), _Practical nonparametric statistics_. New York: John Wiley & Sons. Pages 97-104. Myles Hollander & Douglas A. Wolfe (1973), _Nonparametric Statistical Methods._ New York: John Wiley & Sons. Pages 15-22. _S_e_e _A_l_s_o: 'prop.test' for a general (approximate) test for equal or given proportions. _E_x_a_m_p_l_e_s: ## Conover (1971), p. 97f. ## Under (the assumption of) simple Mendelian inheritance, a cross ## between plants of two particular genotypes produces progeny 1/4 of ## which are "dwarf" and 3/4 of which are "giant", respectively. ## In an experiment to determine if this assumption is reasonable, a ## cross results in progeny having 243 dwarf and 682 giant plants. ## If "giant" is taken as success, the null hypothesis is that p = ## 3/4 and the alternative that p != 3/4. binom.test(c(682, 243), p = 3/4) binom.test(682, 682 + 243, p = 3/4) # The same. ## => Data are in agreement with the null hypothesis. ```