Splus 6.1
– Probability
Jen-Jen Lin
1
常見的的機率密度函數
(連續隨機變數)
X ~ N (µ , σ )
2
• 常態機率分配
• 標準常態機率分配
• t-分配
• 卡方分配
• F分配
Z ~ N ( µ = 0, σ = 1)
2
T ~ t − dist (df )
χ ~ Chi − Square(df )
2
F ~ F − dist (df1 , df 2 )
2
Distributions
distribution S name
normal
norm
parameters
mean, sd
T
df
t
Chi-Square chisq
df
F
df1,df2
f
3
標準常態機率分配
#
# Standard NormaL Distribution, mean=0, sd=1
#
# (1) get the histogram of random normal samples
x<-rnorm(1000,0,1)
hist(x,ylab="probability",main="Histogra
m of N(0,1)",breaks=seq(from=-5,
to=5, length=21), pro=T,ylim=c(0,0.5))
4
標準常態機率分配
#
# Add Normal density function curve on the histogram
#
y<-seq(from=-4,to=4,length=300)
lines(y,dnorm(y,0,1))
5
標準常態機率分配
6
常態機率分配
#
# NormaL Distribution, mean=100, sd=3
#
# (2) get the histogram of random normal samples
x<-rnorm(1000,100,3)
hist(x,ylab="probability",main="Histogram of
N(mean=100,sd=3)",breaks=seq(from=80, to=120,
length=41), pro=T,ylim=c(0,0.2))
#
# Add Normal density function curve on the histogram
y<-seq(from=82,to=118,length=300)
lines(y,dnorm(y,100,3))
7
常態機率分配(mean=100,sd=3)
8
卡方機率分配
#
# Chi-Square Distribution, df =5
#
chi2df <- 5
x<-rchisq(1000,df=chi2df)
hist(x,ylab="probability",main=paste("Histogram of
Chi2(df=",chi2df,")"),breaks=seq(from=0, to=120,
length=41), pro=T,ylim=c(0,0.2))
#
# Add Chi-Square density function curve on the
# histogram
y<-seq(from=0,to=120,length=300)
lines(y,dchisq(y,chi2df))
9
卡方機率分配(df=5)
10
T 機率分配
# t Distribution, df =5
#
tdf <- 5
x<-rt(1000,df=tdf)
hist(x,ylab="probability",main=paste("Histogram of tdist(df=",tdf,")"),breaks=seq(from=-10, to=10,
length=41), pro=T,ylim=c(0,0.5))
#
# Add t density function curve on the histogram
y<-seq(from=-10,to=10,length=300)
lines(y,dt(y,tdf),col=4)
11
T 機率分配
#
# compare with the normal lin
# Add the normal line on the t-distribution
#
z<-seq(from=-3,to=3,length=300)
lines(z,dnorm(z,0,1),col=2)
#
# add the description on the figure
#
legend(2.5,0.45,c(": stand normal dist",": tdist"),col=c(2,4),lty=c(1,1), bg='gray95')
12
T 機率分配(df=5)
13
F 機率分配
# F Distribution, df1 = 7, df2= 10
#
# (4) get the histogram of random F-dist samples
fdf1 <- 7
fdf2 <- 10
x<-rf(1000,df1=fdf1,df2=fdf2)
hist(x,ylab="probability",main=paste("Histogram of Fdist(df1=",fdf1,", df2=",fdf2,")"),breaks=seq(from=0,
to=30, length=61), pro=T,ylim=c(0,0.9))
#
# Add F density function curve on the histogram
y<-seq(from=0,to=15,length=300)
lines(y,df(y,fdf1,fdf2),col=8)
14
F機率分配(df1=7,df2=10)
15
Finding Probability
R name
d+S name
p+S name
q+S name
r+S name
description
height of density height
at x.
given q, find cumulative
prob on (-inf, q)
given cum.prob p, find
quantile
generate n random
sample
16
Normal Distribution
Norm distribution
S name
dnorm(x, mean=0, sd=1) height of density height
at x.
pnorm(q, mean=0, sd=1) given q, find cumulative
prob on (-inf, q)
qnorm(p, mean=0, sd=1) given cum.prob p, find
quantile
rnorm(n,0,1)
generate n random
sample
17
Chi2 Distribution
Norm distribution
dchisq(x, df)
pchisq(q, df)
qchisq(p, df)
rchisq(p, df)
S name
height of density height
at x.
given q, find cumulative
prob on (-inf, q)
given cum.prob p, find
quantile
generate n random
sample
18
T Distribution
Norm distribution
dt(x, df)
pt(q, df)
qt(p, df)
rt(p, df)
S name
height of density height
at x.
given q, find cumulative
prob on (-inf, q)
given cum.prob p, find
quantile
generate n random
sample
19
F Distribution
Norm distribution
df(x, df)
pf(q, df)
qf(p, df)
rf(p, df)
S name
height of density height
at x.
given q, find cumulative
prob on (-inf, q)
given cum.prob p, find
quantile
generate n random
sample
20
(1) height of density height at one point
#
# (1) height of density height at one point
#
x <- 0
dnorm(x, mean=0, sd=1)
# height of density height point x=0
#
# check the N(0,1)density height at x=0
1/(sqrt(2*pi))*exp(0)
21
(1) height of density height at one point
22
(2) cumulative prob on (-inf, q)
# (2) cumulative prob on (-inf, q)
#
q <- 0
pnorm(q, mean=0, sd=1)
# cumulative prob on (-inf, 0)
q <- 1.96
pnorm(q, mean=0, sd=1)
# cumulative prob on (-inf, 1.96)
23
(2) cumulative prob on (-inf, q)
24
(3) given cumulative prob p, find
the corresponding quantile
#
# (3) given cumulative prob p, find the corresponding quantile
#
p <- 0.5
qnorm(p, mean=0, sd=1)
# cumulative prob p=0.5, the corresponding quantile
p <- 0.975
qnorm(p, mean=0, sd=1)
# cumulative prob p=0.975, the corresponding quantile
25
(3) given cumulative prob p, find
the corresponding quantile
26
(4) generate n random sample
#
# (4) generate n random sample
n <- 10
rnorm(n,0,1)
# generate n=10 random sample from N(0,1)
n <- 15
rnorm(n, mean=10, sd=2)
# generate n=15 random sample from N(10,sd=2)
27
(4) generate n random sample
28
© Copyright 2026 Paperzz