sadII_lab3_test_chi

2450 days ago by abogdanowicz

#Tryb %r ^ # Przetestuj rozklad cyfr w rozwinieciu dziesietnym liczby pi na jednostajnosc testem # goodness-of-fit ChiSq test library(lattice) #Set output widths better .adjustWidth <- function(...){ options(width=10000) ; TRUE} .adjustWidthCallBack <- addTaskCallback(.adjustWidth) 
       
.adjustWidth <- function(...){ options(width=10000) ; TRUE} 
library(Rmpfr) # biblioteka do dowolnie duzej precyzji liczb rzeczywistych 
       
Loading C code of R package 'Rmpfr': GMP using 64 bits per limb

Attaching package: 'Rmpfr'


	The following object(s) are masked from package:stats :

	 pnorm 


	The following object(s) are masked from package:base :

	 cbind,
	 pmax,
	 pmin,
	 rbind 
n.digits=2000 base=10 bits.of.precision <- n.digits*log2(base) Pi<-Const("pi", bits.of.precision) 
       
nchar(as(Pi,"character",2)) 
       
[1] 2002
give.digits<-function(Pi){ digits<-strsplit(as(Pi,"character"),"")[[1]] digits<-digits[digits %in% as.character(0:9)] # cyfry bez kropki - zwroc uwage na operator %in% as.numeric(digits) } 
       
# Oto tabela licznosci cyfr w rozwinieciu Pi table(factor(give.digits(Pi))) 
       
  0   1   2   3   4   5   6   7   8   9 
182 212 207 189 195 205 200 198 202 211 
#Przetestuj licznosci uzywajac chisq.test i podajac mu odpowiednie argumenty chisq.test(table(give.digits(Pi))) 
       
	Chi-squared test for given probabilities

data:  table(give.digits(Pi)) 
X-squared = 4.0825, df = 9, p-value = 0.9059
# Przetestuj podobnie rozwiniecie "e" table(factor(give.digits( exp(mpfr(1,1e5 )) ))) chisq.test(table(give.digits( exp(mpfr(1,1e5 ))))) 
       
   0    1    2    3    4    5    6    7    8    9 
2981 3046 3028 3056 2999 2977 3085 3007 2997 2928 

	Chi-squared test for given probabilities

data:  table(give.digits(exp(mpfr(1, 1e+05)))) 
X-squared = 6.083, df = 9, p-value = 0.7316
# Przetestuj teraz liczbe wymierna 5/133 table(factor(give.digits( mpfr(5,1e5)/mpfr(133,1e5) ))) 
       
   0    1    2    3    4    5    6    7    8    9 
5018 1672 1672 3346 3344 3346 3344 1673 1673 5018 
chisq.test( table( give.digits( mpfr(5,1e5)/mpfr(133,1e5) ))) 
       
	Chi-squared test for given probabilities

data:  table(give.digits(mpfr(5, 1e+05)/mpfr(133, 1e+05))) 
X-squared = 5204.496, df = 9, p-value < 2.2e-16
table(factor(give.digits( mpfr(5,1e5)/mpfr(133,1e5) ) )) 
       
   0    1    2    3    4    5    6    7    8    9 
5018 1672 1672 3346 3344 3346 3344 1673 1673 5018 
#Przyjzyj sie rozwinieciu dziesietnemu 5/133 i wykonaj test Chi2 zgodnie z wlasciwym rozkladem cyfr mpfr(5,200)/mpfr(133,200) 
       
1 'mpfr' number of precision  200   bits 
[1]
0.037593984962406015037593984962406015037593984962406015037593969
p <- table(c(0,3,7,5,9,3,9,8,4,9,6,2,4,0,6,0,1,5))/18 p sum(p) chisq.test( table( give.digits( mpfr(500,200)/mpfr(133,200) )), p=p) 
       
         0          1          2          3          4          5   
6          7          8          9 
0.16666667 0.05555556 0.05555556 0.11111111 0.11111111 0.11111111
0.11111111 0.05555556 0.05555556 0.16666667 
[1] 1

	Chi-squared test for given probabilities

data:  table(give.digits(mpfr(500, 200)/mpfr(133, 200))) 
X-squared = 1.1452, df = 9, p-value = 0.999

Warning message:
In chisq.test(table(give.digits(mpfr(500, 200)/mpfr(133, 200))),  :
  Chi-squared approximation may be incorrect
# Wykonaj recznie test goodnes of fit Pearsona przy pomocy #?qchisq #http://en.wikipedia.org/wiki/Goodness_of_fit#Pearson.27s_chi-squared_test 
       
# Wykonaj recznie test goodnes of fit Pearsona przy pomocy  
chisq.pval <- function(zliczenia, p, chi.df){ chi2 <- sum( ((zliczenia-sum(zliczenia*p))^2) / (sum(zliczenia)*p) ) 1 - 2 * abs(pchisq(chi2, chi.df) - 0.5) } 
       
chisq.pval(table(give.digits(Pi)), rep(0.1, 10), 9) 
       
[1] 0.1881963