Code:
3 poisson2.txt text/plain 0.19 KB
poisson = function(k,lambda)
{
POS=(k>-1)
if(POS){
fact = 1
NONUL<-(k!=0)
if(NONUL){
for(i in 2 : k){fact=fact*i}} else {fact=1}
poisson=exp(-lambda)*lambda^k/fact}
else{poisson=0}
}
4 poisson.txt text/plain 0.17 KB
poisson = function(k,lambda)
{
factoriel = function(n)
{if(n==1 || n==0)
1
else
n*factoriel(n-1)
}
poisson=exp(-lambda)*((lambda^k)/factoriel(k))}
5 TP_Stat.txt text/plain 4.68 KB
aya_folie@hotmail.com
getwd() > donne chemin courant
setwd("c:/")
q()
ls()
rm()
save:
x <- runif(20)
y <- list(a = 1, b = TRUE, c = "oops")
save(x, y, file = "xy.Rdata")
> a=5
> cat("a vaut", a,"\n")
a vaut 5
file >change dir
/****************************************/
toto = function (x,y,...)
{toto = x^2+y}
> source("toto.txt")
> t=toto(2,1)
> t
[1] 5
/****************************************/
I]
b/
Loi poisson
f(k,lambda)=P(N=k)=(e^-L) * (L^k)/k!
poisson = function(k,lambda)
{
factoriel = function(n)
{if(n==1 || n==0)
1
else
n*factoriel(n-1)
}
poisson=exp(-lambda)*((lambda^k)/factoriel(k))}
a/
type evt rare
> source("poisson.txt")
> t=poisson(5,8 )
> t
[1] 0.09160366
c/
EnfantFrance2 = poisson(2,1.9) >> 26.9%
EnfantFrance4 = poisson(4,1.9) >> 8%
EnfantItalie2 = poisson(2,1.1) >> 20%
EnfantItalie4 = poisson(4,1.1) >> 2%
/****************************************/
II]
binomial
b(x=k)=C n,p P^k (1-p)^(n-k)
/****************************************/
III
> x=c(1,2,3,4,5,6)
> y=c(0,1)
> z=c(x,y)
> mat=matrix(z,2,4)
source("poisson.txt")
lapply(m1,poisson)
poisson = function(k)
{
factoriel = function(n)
{if(n==1 || n==0)
1
else
n*factoriel(n-1)
}
lambda=1
poisson=exp(-lambda)*((lambda^k)/factoriel(k))}
/****************************************/
> mat
[,1] [,2] [,3] [,4]
[1,] 1 3 5 0
[2,] 20 40 60 1
> apply(mat,2,sum)
[1] 21 43 65 1
> apply(mat,1,sum)
[1] 9 121
> sum(mat)
[1] 130
> dim(mat)
[1] 2 4
> nrow(mat)
[1] 2
> ncol(mat)
[1] 4
> t(mat)
[,1] [,2]
[1,] 1 20
[2,] 3 40
[3,] 5 60
[4,] 0 1
> solve(mat) si mat carré
> solve(mat,mat2)
> diag(mat)
> diag(2)
> mat[mat>3] donne val > à ..
> mat[mat>30 & mat<60]
/****************************************/
6 Actions differentes
Ai i=1,...6
Mr1 Mr2 Mr3 Mr4 Mr5 Mr6
A1 10 5 20 6 2 20
A2 0 5 20 6 2 20
A3 10 5 5 10 2 20
A4 10 5 0 10 5 30
A5 0 5 0 8 0 30
A6 50 10 5 8 0 0
5440 1485 780 1618 184 2740
> t<-read.table("Mr.txt")
> t<-as.matrix(t)
> t(t)
> t
A1 A2 A3 A4 A5 A6 A7
Mr1 10 0 10 10 0 50 5440
Mr2 10 10 10 10 10 10 1485
Mr3 20 20 5 0 0 5 780
Mr4 6 6 10 10 8 8 1618
Mr5 2 2 2 5 30 0 184
Mr6 20 20 20 30 30 0 2740
> is(t)
[1] "matrix" "structure" "array" det
> class(t)
[1] "matrix"
> b=t[7,]
> b
Mr1 Mr2 Mr3 Mr4 Mr5 Mr6
5440 1485 780 1618 184 2740
/************************/
10P1 + 5P2 = 1000
5P1 + 2P2 = 800
P1 ? P2 ?
10 5 P1 = 1000
5 2 P2 800
/************************/
> a=t[1:6,]
> a=t(a)
> a
A1 A2 A3 A4 A5 A6
Mr1 10 0 10 10 0 50
Mr2 5 5 5 5 5 10
Mr3 20 20 5 0 0 5
Mr4 6 6 10 10 8 8
Mr5 2 2 2 5 0 0
Mr6 20 20 20 30 30 0
> b=t[7,]
> b
Mr1 Mr2 Mr3 Mr4 Mr5 Mr6
[1,] 5440 1485 780 1618 184 2740
> solve(a,t(b))
S
A1 10
A2 3
A3 4
A4 30
A5 50
A6 100
/********************************/
> t
A1 A2 A3 A4 A5 A6 S
Mr1 10 0 10 10 0 50 5440
Mr2 5 5 5 5 5 10 1485
Mr3 20 20 5 0 0 5 780
Mr4 6 6 10 10 8 8 1618
Mr5 2 2 2 5 0 0 184
Mr6 20 20 20 30 30 0 2740
> a
A1 A2 A3 A4 A5 A6
Mr1 10 0 10 10 0 50
Mr2 5 5 5 5 5 10
Mr3 20 20 5 0 0 5
Mr4 6 6 10 10 8 8
Mr5 2 2 2 5 0 0
Mr6 20 20 20 30 30 0
> b
Mr1 Mr2 Mr3 Mr4 Mr5 Mr6
5440 1485 780 1618 184 2740
> solve(a,t(b))
Error in "rownames<-"(`*tmp*`, value = colnames(a)) :
length of dimnames [1] not equal to array extent
> solve(a,b)
Mr1 Mr2 Mr3 Mr4 Mr5 Mr6
10 3 4 30 50 100
/********************************/
2.
poisson = function(k,lambda)
{
POS=(k>-1)
if(POS){
fact = 1
NONUL<-(k!=0)
if(NONUL){
for(i in 2 : k){fact=fact*i}} else {fact=1}
poisson=exp(-lambda)*lambda^k/fact}
else{poisson=0}
}
> source("poisson2.txt")
> me<-poisson(-1); me
[1] 0
> me<-poisson(-1)
> me
[1] 0
> me<-poisson(-1,1); me
[1] 0
> me<-poisson(3,1); me
[1] 0.06131324
> me<-poisson(0,1); me
[1] 0.3678794
> k=seq(-10,10)
> lamb1=rep(1,21)
> lamb2=rep(5,21)
> lamb3=rep(10,21)
> p=cbind(k,lamb1)
> k
[1] -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
[19] 8 9 10
> p
k lamb1
[1,] -10 1
[2,] -9 1
[3,] -8 1
[4,] -7 1
[5,] -6 1
[6,] -5 1
[7,] -4 1
[8,] -3 1
[9,] -2 1
[10,] -1 1
[11,] 0 1
[12,] 1 1
[13,] 2 1
[14,] 3 1
[15,] 4 1
[16,] 5 1
[17,] 6 1
[18,] 7 1
[19,] 8 1
[20,] 9 1
[21,] 10 1
>
6 Mr.txt text/plain 0.16 KB
Mr1 Mr2 Mr3 Mr4 Mr5 Mr6
A1 10 5 20 6 2 20
A2 0 5 20 6 2 20
A3 10 5 5 10 2 20
A4 10 5 0 10 5 30
A5 0 5 0 8 0 30
A6 50 10 5 8 0 0
S 5440 1485 780 1618 184 2740