Skip to content

Commit 366ae9c

Browse files
committed
Clean up
1 parent bc088ad commit 366ae9c

25 files changed

+1766
-0
lines changed

study/R/basic.r

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/evn Rscript
2+
# Vector
3+
message("== Vector ==")
4+
A<-c(1,2,3,4,5)
5+
print(A)
6+
A<-1:5
7+
print(A)
8+
print(length(A))
9+
# Label
10+
message("== Labeling ==")
11+
names(A)<-c('aa','bb','cc','dd','ee')
12+
print(A)
13+
# Matrix
14+
message("== Matrix ==")
15+
B<-matrix(0:9,2,5)
16+
message("== dim(B) ==")
17+
print(dim(B))
18+
message("== B ==")
19+
print(B)
20+
B[1,2]<-99
21+
message("== B[1,] ==")
22+
B[1,]
23+
message("== B[,3] ==")
24+
B[,3]
25+
print(B)
26+
# Matrix label
27+
message("== Matrix labeling ==")
28+
colnames(B)<-c('aa','bb','cc','dd','ee')
29+
rownames(B)<-c('XX','YY')
30+
message("== B(labeled) ==")
31+
print(B)
32+
# Array
33+
message("== Array ==")
34+
C<-array(1:30,c(2,5,3))
35+
print(C)
36+
print(C[1,4,2])
37+
# List
38+
message("== List ==")
39+
D<-list(A,B,C)
40+
print(D)
41+
print(D[[2]])
42+
print(D[[3]][2,5,3])
43+
44+
# Write
45+
message("== Write ==")
46+
write(A,"A.txt")
47+
write.table(t(A),"At.txt")
48+
write(B,"B.txt")
49+
write.table(B,"Bt.txt")
50+
write(C,"C.txt")
51+
52+
for(i in 1:dim(C)[3]){
53+
write(t(C[,,i]),paste("Ct",i,".txt",sep=""))
54+
}
55+
save(D,file="Ds.txt")
56+
57+
# Read
58+
message("== Read ==")
59+
read.table("At.txt")
60+
read.table("Bt.txt")
61+
matrix(scan("B.txt"),2,5,byrow=T)
62+
data.frame(scan("B.txt",what=list(aa=0,bb=0,cc=0,dd=0,ee=0)))
63+
64+
Cr<-array(0,c(2,5,3))
65+
Cr[,,1]=matrix(scan("Ct1.txt"),2,5,byrow=T)
66+
Cr[,,2]=matrix(scan("Ct2.txt"),2,5,byrow=T)
67+
Cr[,,3]=matrix(scan("Ct3.txt"),2,5,byrow=T)
68+
print(Cr)
69+
70+
rm(D)
71+
load("Ds.txt")
72+
print(D)

study/R/calc.r

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/evn Rscript
2+
message("== Data ==")
3+
B<-matrix(0:24,5,5)
4+
colnames(B)<-c('C1','C2','C3','C4','C5')
5+
rownames(B)<-c('R1','R2','R3','R4','R5')
6+
B[1,2]<-99
7+
message("== B ==")
8+
B
9+
message("== B*2 ==")
10+
B*2
11+
message("== B+matrix(100:129,5,5) ==")
12+
B+matrix(100:129,5,5)
13+
message("== matrix(c(10,20,30,40,50),5,1)*c(1,2,3,4,5) ==")
14+
matrix(c(10,20,30,40,50),5,1)*c(1,2,3,4,5)
15+
message("== B*c(1,2,3,4,5) ==")
16+
B*c(1,2,3,4,5)
17+
18+
message("== SUM col ==")
19+
apply(B,1,sum)
20+
message("== MEAN col ==")
21+
apply(B,1,mean)
22+
message("== SUM row ==")
23+
apply(B,2,sum)
24+
message("== MY row ==")
25+
my<-function(x){
26+
x-mean(x)
27+
}
28+
apply(B,2,my)

study/R/cluster.r

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/evn Rscript
2+
library(rpart)
3+
library(randomForest)
4+
library(e1071)
5+
data(iris)
6+
d<-iris
7+
plot(d[,1], d[,3], col=c(2,3,4)[unclass(d$Species)],main="Origin")
8+
9+
message("=== RandomForest ===")
10+
forest <- randomForest(Species~.,data=d,ntree=500,importance=T)
11+
predforest <- predict(forest,d,type="class")
12+
table(d$Species,predforest)
13+
plot(d[,1], d[,3], col=c("red","blue","green")[unclass(predforest)],main="Forest")
14+
15+
message("=== Rpart ===")
16+
tree <- rpart(Species~.,data=d,method="class")
17+
predtree <- predict(tree,d,type="class")
18+
table(d$Species,predtree)
19+
plot(d[,1], d[,3], col=c("red","blue","green")[unclass(predtree)],main="Rpart")
20+
21+
message("=== Cmeans ===")
22+
r<-cmeans(d[,-5],3,150,verbose=F,method="cmeans",m=2)
23+
c<-apply(r$membership,1,function(x){if ( max(x) < 0.6 ) { which.max(x)+10 }else{ which.max(x)}} )
24+
table(d$Species,c)
25+
c<-apply(r$membership,1,function(x){rgb(x[1],x[2],x[3])})
26+
plot(d[,1], d[,3], col=c,main="Cmeans")
27+
28+
message("=== Ufcl ===")
29+
r<-cmeans(d[,-5],3,150,verbose=F,method="ufcl",m=2)
30+
c<-apply(r$membership,1,function(x){if ( max(x) < 0.6 ) { which.max(x)+10 }else{ which.max(x)}} )
31+
table(d$Species,c)
32+
c<-apply(r$membership,1,function(x){rgb(x[1],x[2],x[3])})
33+
plot(d[,1], d[,3], col=c,main="Ufcl")
34+
35+
message("=== Kmeans hartigan-wong ===")
36+
k<-kmeans(d[,-5],3,iter.max=10,nstart=1,algorithm="Hartigan-Wong")
37+
table(d$Species,k$cluster)
38+
plot(d[,1], d[,3], col=k$cluster,main="Kmeans hartigan-wong")
39+
40+
message("=== average ===")
41+
h<-hclust(dist(d[,-5]),method = "average")
42+
table(d$Species,cutree(h,3))
43+
plot(d[,1], d[,3], col=cutree(h,3),main="average")
44+
h<-hclust(dist(d[,-5])^2,method = "average")
45+
table(d$Species,cutree(h,3))
46+
plot(d[,1], d[,3], col=cutree(h,3),main="average^2")
47+
message("=== complete ===")
48+
h<-hclust(dist(d[,-5]),method = "complete")
49+
table(d$Species,cutree(h,3))
50+
plot(d[,1], d[,3], col=cutree(h,3),main="complete")
51+
h<-hclust(dist(d[,-5])^2,method = "complete")
52+
table(d$Species,cutree(h,3))
53+
plot(d[,1], d[,3], col=cutree(h,3),main="complete^2")
54+
message("=== median ===")
55+
h<-hclust(dist(d[,-5]),method = "median")
56+
table(d$Species,cutree(h,3))
57+
plot(d[,1], d[,3], col=cutree(h,3),main="median")
58+
message("=== ward ===")
59+
h<-hclust(dist(d[,-5]),method = "ward")
60+
table(d$Species,cutree(h,3))
61+
plot(d[,1], d[,3], col=cutree(h,3),main="ward")
62+
h<-hclust(dist(d[,-5])^2,method = "ward")
63+
table(d$Species,cutree(h,3))
64+
plot(d[,1], d[,3], col=cutree(h,3),main="ward^2")
65+
66+

study/R/graph.r

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/evn Rscript
2+
data(iris)
3+
# PLOT
4+
plot(iris[,1],iris[,3])
5+
# PLOT VAL
6+
plot(iris[,1],iris[,3],type='n')
7+
text(iris[,1],iris[,3])
8+
# PLOT TXT
9+
plot(iris[,1],iris[,3],type='n')
10+
text(iris[,1],iris[,3],rep(c("S","C","V"),rep(50,3)))
11+
# BIG
12+
plot(iris[,1],iris[,3],type="p",xlab="Length of Sepal", ylab="Length of Petal",cex=2,col="red")
13+
# Color
14+
plot(iris[,1],iris[,3],pch = 21, cex=0.5,bg = c(2, 3, 4)[unclass(iris$Species)],col=c(2,3,4)[unclass(iris$Species)])
15+
# Cloud
16+
library(lattice)
17+
cloud(Sepal.Length ~ Petal.Length * Petal.Width, data = iris,groups=Species,pch=c(16,17,18),cex=0.5,bg = c(2, 3, 4),col=c(2,3,4))
18+
cloud(Sepal.Length ~ Petal.Length * Petal.Width, data = iris,groups=Species,pch=c(16,17,18),cex=0.5,bg = c(2, 3, 4),col=c(2,3,4),screen=list(z=0,x=10,y=-15))
19+

study/R/predict.r

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/evn Rscript
2+
library(rpart)
3+
library(rpart.plot)
4+
data(iris)
5+
d<-iris
6+
s <- sample(nrow(d),nrow(d)*0.8)
7+
d.sample <- d[s,]
8+
d.test <- d[-s,]
9+
10+
11+
tree <- rpart(Species~.,data=d.sample,method="class")
12+
predtree <- predict(tree,d.test,type="class")
13+
14+
table(predtree,d.test$Species)
15+
rpart.plot(tree,yesno=F,extra=101,type=4,faclen=15,varlen=15,fallen.leaves=T,snip=T)

study/R/randomForest.r

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/evn Rscript
2+
library(randomForest)
3+
data(iris)
4+
d<-iris
5+
s <- sample(nrow(d),nrow(d)*0.5)
6+
d.sample <- d[s,]
7+
d.test <- d[-s,]
8+
9+
forest <- randomForest(Species~.,data=d.sample,ntree=500,importance=T)
10+
forest
11+
plot(forest)
12+
varImpPlot(forest)
13+
importance(forest)
14+
15+
predforest <- predict(forest,d.test,type="class")
16+
table(predforest,d.test$Species)

study/R/rpart.r

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/evn Rscript
2+
library(rpart.plot)
3+
data(iris)
4+
tree <- rpart(Species~.,data=iris,method="class")
5+
rpart.plot(tree,yesno=F,extra=101,type=4,faclen=15,varlen=15)
6+
rpart.plot(tree,yesno=F,extra=101,type=4,faclen=15,varlen=15,fallen.leaves=T,snip=T)
7+
8+
rpart.plot(tree,branch.type=4,yesno=F,faclen=15,varlen=15,extra=101)
9+

0 commit comments

Comments
 (0)