Posterior Probability Computation

advertisement
R version 2.9.2 (2009-08-24)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Previously saved workspace restored]
> data(Cushings)
Warning message:
In data(Cushings) : data set 'Cushings' not found
> plot(Cushings[,1:2], col=unclass(Cushings[,3]),
+ pch=as.character(Cushings[,3]))
Error in plot(Cushings[, 1:2], col = unclass(Cushings[, 3]), pch
= as.character(Cushings[, :
object 'Cushings' not found
> Cushings
Error: object 'Cushings' not found
>
> library(MASS)
> data(Cushings)
> plot(Cushings[,1:2], col=unclass(Cushings[,3]),
+ pch=as.character(Cushings[,3]))
> Cushings
Tetrahydrocortisone Pregnanetriol Type
a1
3.1
11.70
a
a2
3.0
1.30
a
a3
1.9
0.10
a
a4
3.8
0.04
a
a5
4.1
1.10
a
a6
1.9
0.40
a
b1
8.3
1.00
b
b2
3.8
0.20
b
b3
3.9
0.60
b
b4
7.8
1.20
b
b5
9.1
0.60
b
b6
15.4
3.60
b
b7
7.7
1.60
b
b8
6.5
0.40
b
b9
5.7
0.40
b
b10
13.6
1.60
b
c1
10.2
6.40
c
c2
9.2
7.90
c
c3
9.6
3.10
c
c4
53.8
2.50
c
c5
15.8
7.60
c
u1
5.1
0.40
u
u2
12.9
5.00
u
u3
13.0
0.80
u
u4
2.6
0.10
u
u5
30.0
0.10
u
u6
20.5
0.80
u
>
> unk <- (1:27)[Cushings[,3]=="u"]
> cush.ltp <- log(Cushings[-unk,1:2])
> cush.class <- factor(Cushings[-unk,3])
>
> cush.lda <- lda(cush.ltp, cush.class)
> cush.pred.l <- predict(cush.lda, cush.ltp)
> cush.qda <- qda(cush.ltp, cush.class)
> cush.pred.q <- predict(cush.qda, cush.ltp)
>
plot(cush.ltp,pch=unclass(cush.class),col=unclass(cush.pred.q$cl
ass))
> predplot(cush.qda, cush.ltp, cush.class)
Error: could not find function "predplot"
> #decrease len if you have little memory.
> predplot <- function(object, data, class, len = 100, ...)
+ {
+
plot(data,col=unclass(class),pch=unclass(class))
+
xp <- seq(min(data[,1]),max(data[,1]),length=len)
+
yp <- seq(min(data[,2]),max(data[,2]),length=len)
+
grid <- expand.grid(xp,yp)
+
colnames(grid) <- colnames(data)
+
Z <- predict(object, grid, ...);
+
zc <- as.numeric(Z$class)
+ #browser()
+
for (i in unique(zc))
+ {zp <- Z$post[,i] - apply(as.matrix(Z$post[,-i]),1,max)
+
contour(xp, yp, matrix(zp, len),
+
add = T, levels = 0, drawlabels = F)}
+
invisible()
+ }
> predplot(cush.qda, cush.ltp, cush.class)
>
> cush.class
[1] a a a a a a b b b b b b b b b b c c c c c
Levels: a b c
> as.factor(produce$Location)
Error in inherits(x, "factor") : object 'produce' not found
> table(Actual=cush.class, Classified=cush.pred.q$class)
Classified
Actual a b c
a 6 0 0
b 0 9 1
c 0 1 4
> (1+1)/length(cush.class)
[1] 0.0952381
> ?qda
> cush.qda.cv <- qda(cush.ltp, cush.class, CV=T)
> cush.qda.cv
$class
[1] c a a a a a b b a b b c b b b b c c b b c
Levels: a b c
$posterior
a1
a2
a3
a4
a5
a6
b1
b2
b3
b4
b5
b6
b7
b8
b9
b10
c1
c2
c3
c4
c5
a
2.710769e-01
9.995486e-01
7.173135e-01
9.387700e-01
8.075482e-01
9.967018e-01
1.208229e-03
4.941469e-01
8.949635e-01
2.741092e-03
1.326874e-03
1.453647e-06
7.328264e-03
1.637380e-02
3.080219e-02
1.142217e-05
4.771808e-04
1.884260e-03
2.047639e-03
4.568707e-11
2.253315e-06
b
1.063718e-15
4.488013e-04
2.826865e-01
6.122998e-02
1.924446e-01
3.298205e-03
9.987690e-01
5.058531e-01
1.050365e-01
9.971409e-01
9.986728e-01
2.095953e-01
9.896376e-01
9.836262e-01
9.691978e-01
9.684997e-01
1.355948e-02
1.180815e-03
9.979202e-01
9.999990e-01
1.416779e-01
c
7.289231e-01
2.597752e-06
6.383218e-29
9.599874e-34
7.132110e-06
1.782123e-15
2.274741e-05
1.674928e-18
1.064219e-09
1.180411e-04
3.635929e-07
7.904032e-01
3.034166e-03
5.562795e-11
1.314517e-11
3.148891e-02
9.859633e-01
9.969349e-01
3.220436e-05
9.885363e-07
8.583198e-01
$call
qda(x = cush.ltp, cush.class, CV = T)
> table(Actual=cush.class, Classified=cush.pred.q$class)
Classified
Actual a b c
a 6 0 0
b 0 9 1
c 0 1 4
>
> table(Actual=cush.class, Classified=cush.qda.cv$class)
Classified
Actual a b c
a 5 0 1
b 1 8 1
c 0 2 3
> (1+1)/length(cush.class)
[1] 0.0952381
> (5)/length(cush.class)
[1] 0.2380952
> cush.ltp
Tetrahydrocortisone Pregnanetriol
a1
1.1314021
2.45958884
a2
1.0986123
0.26236426
a3
0.6418539
-2.30258509
a4
1.3350011
-3.21887582
a5
1.4109870
0.09531018
a6
0.6418539
-0.91629073
b1
2.1162555
0.00000000
b2
1.3350011
-1.60943791
b3
1.3609766
-0.51082562
b4
2.0541237
0.18232156
b5
2.2082744
-0.51082562
b6
2.7343675
1.28093385
b7
2.0412203
0.47000363
b8
1.8718022
-0.91629073
b9
1.7404662
-0.91629073
b10
2.6100698
0.47000363
c1
2.3223877
1.85629799
c2
2.2192035
2.06686276
c3
2.2617631
1.13140211
c4
3.9852735
0.91629073
c5
2.7600099
2.02814825
> x=cush.ltp[1,]
> x
Tetrahydrocortisone Pregnanetriol
a1
1.131402
2.459589
> cush.qda.cv$posterior[1,]
a
b
c
2.710769e-01 1.063718e-15 7.289231e-01
> round(cush.qda.cv$posterior[1,],3)
a
b
c
0.271 0.000 0.729
> ans=round(cush.qda.cv$posterior[1,],3)
> cush.ltp[-1,]
Tetrahydrocortisone Pregnanetriol
a2
1.0986123
0.26236426
a3
0.6418539
-2.30258509
a4
1.3350011
-3.21887582
a5
1.4109870
0.09531018
a6
0.6418539
-0.91629073
b1
2.1162555
0.00000000
b2
1.3350011
-1.60943791
b3
1.3609766
-0.51082562
b4
2.0541237
0.18232156
b5
2.2082744
-0.51082562
b6
2.7343675
1.28093385
b7
2.0412203
0.47000363
b8
1.8718022
-0.91629073
b9
1.7404662
-0.91629073
b10
2.6100698
0.47000363
c1
2.3223877
1.85629799
c2
2.2192035
2.06686276
c3
2.2617631
1.13140211
c4
3.9852735
0.91629073
c5
2.7600099
2.02814825
> x1=cush.ltp[-1,cush.class=='a']
Error in `[.data.frame`(cush.ltp, -1, cush.class == "a") :
undefined columns selected
> x1=cush.ltp[-1,cush.class=="a"]
Error in `[.data.frame`(cush.ltp, -1, cush.class == "a") :
undefined columns selected
> x1=cush.ltp[cush.class,-1]=='a',]
Error: unexpected ',' in "x1=cush.ltp[cush.class,-1]=='a',"
> x1=cush.ltp[cush.class,-1]=="a",]
Error: unexpected ',' in "x1=cush.ltp[cush.class,-1]=="a","
> x1=cush.ltp[-1,][1:5,]
> x1
Tetrahydrocortisone Pregnanetriol
a2
1.0986123
0.26236426
a3
0.6418539
-2.30258509
a4
1.3350011
-3.21887582
a5
1.4109870
0.09531018
a6
0.6418539
-0.91629073
> x2=cush.ltp[-1,][1:5+10,]
> x2
Tetrahydrocortisone Pregnanetriol
b6
2.734368
1.2809338
b7
2.041220
0.4700036
b8
1.871802
-0.9162907
b9
1.740466
-0.9162907
b10
2.610070
0.4700036
> x2=cush.ltp[-1,][6:15,]
> x2
Tetrahydrocortisone Pregnanetriol
b1
2.116256
0.0000000
b2
1.335001
-1.6094379
b3
1.360977
-0.5108256
b4
2.054124
0.1823216
b5
2.208274
-0.5108256
b6
2.734368
1.2809338
b7
2.041220
0.4700036
b8
1.871802
-0.9162907
b9
1.740466
-0.9162907
b10
2.610070
0.4700036
> x3=cush.ltp[-1,][16:21,]
> x3
Tetrahydrocortisone Pregnanetriol
c1
2.322388
1.8562980
c2
2.219203
2.0668628
c3
2.261763
1.1314021
c4
3.985273
0.9162907
c5
2.760010
2.0281482
NA
NA
NA
> x3=cush.ltp[-1,][16:20,]
> x1;x2;x3
Tetrahydrocortisone Pregnanetriol
a2
1.0986123
0.26236426
a3
0.6418539
-2.30258509
a4
1.3350011
-3.21887582
a5
1.4109870
0.09531018
a6
0.6418539
-0.91629073
Tetrahydrocortisone Pregnanetriol
b1
2.116256
0.0000000
b2
1.335001
-1.6094379
b3
1.360977
-0.5108256
b4
2.054124
0.1823216
b5
2.208274
-0.5108256
b6
2.734368
1.2809338
b7
2.041220
0.4700036
b8
1.871802
-0.9162907
b9
1.740466
-0.9162907
b10
2.610070
0.4700036
Tetrahydrocortisone Pregnanetriol
c1
2.322388
1.8562980
c2
2.219203
2.0668628
c3
2.261763
1.1314021
c4
3.985273
0.9162907
c5
2.760010
2.0281482
> mean(x1)
Tetrahydrocortisone
Pregnanetriol
1.025662
-1.216015
> var(x1)
Tetrahydrocortisone Pregnanetriol
Tetrahydrocortisone
0.13602627
0.07389231
Pregnanetriol
0.07389231
2.29677492
> s1=var(x1)
> s2=var(x2)
> s3=var(x3)
> s=(4*s1+9*s2+4*s3)/(4+9+4)
> s
Tetrahydrocortisone Pregnanetriol
Tetrahydrocortisone
0.2748125
0.1319893
Pregnanetriol
0.1319893
0.9896334
> var(cush.ltp[-1,])
Tetrahydrocortisone Pregnanetriol
Tetrahydrocortisone
0.6241786
0.7274309
Pregnanetriol
0.7274309
1.9703983
> p1=5/20
> p3=5/20
> p2=10/20
> p1;p2;p3
[1] 0.25
[1] 0.5
[1] 0.25
> library(mvtnorm)
> ?dmvnorm
> p1*dmvnorm(x,mean=mean(x1),sigma=s)
a1
6.422051e-05
> j1=p1*dmvnorm(x,mean=mean(x1),sigma=s)
> j2=p2*dmvnorm(x,mean=mean(x2),sigma=s)
> j3=p3*dmvnorm(x,mean=mean(x3),sigma=s)
> j1;j2;j3
a1
6.422051e-05
a1
0.00022839
a1
0.0002064751
> j=j1+j2+j3
> j1/j;j2/j;j3/j
a1
0.1286763
a1
0.4576169
a1
0.4137068
> ans
a
b
c
0.271 0.000 0.729
> j1=p1*dmvnorm(x,mean=mean(x1),sigma=s)
> j1=p1*dmvnorm(x,mean=mean(x1),sigma=var(x1))
> j2=p2*dmvnorm(x,mean=mean(x2),sigma=var(x2))
> j3=p3*dmvnorm(x,mean=mean(x3),sigma=var(x3))
> j=j1+j2+j3
> j1/j;j2/j;j3/j
a1
0.2365863
a1
1.114050e-15
a1
0.7634137
>
Download