Library
library(ggvis)
library(shapes)
##
## Attaching package: 'shapes'
## The following object is masked from 'package:stats':
##
## sigma
library(matlib)
library(rgeos)
## rgeos version: 0.3-23, (SVN revision 546)
## GEOS runtime version: 3.5.0-CAPI-1.9.0 r4084
## Linking to sp version: 1.2-4
## Polygon checking: TRUE
library(plyr)
library(optR)
## Loaded optR Version: 1.2.5
library(randomForest)
## randomForest 4.6-12
## Type rfNews() to see new features/changes/bug fixes.
Set resporitory and read data
setwd("C:/Users/Admin/Documents/Intern/practice")
data <- read.csv("MyData.csv")
data1 <- data.matrix(data[,c("ID","X","Y","isBoundary","isLandmark")])
head(data)
## X Y IMAGE ID Scale isBoundary isLandmark
## 1 193 1053 F_ST03_07.21.11_LT_02.tif 4 0.001099 1 0
## 2 580 1127 F_ST03_07.21.11_LT_02.tif 4 0.001099 1 0
## 3 1173 1229 F_ST03_07.21.11_LT_02.tif 4 0.001099 1 0
## 4 1619 1285 F_ST03_07.21.11_LT_02.tif 4 0.001099 1 0
## 5 1697 1278 F_ST03_07.21.11_LT_02.tif 4 0.001099 1 0
## 6 2106 1229 F_ST03_07.21.11_LT_02.tif 4 0.001099 1 0
Find the centroid by the average of the boundary points
centroids <- aggregate(data[, 1:2], list(data$ID, data$isBoundary ), mean)
centroid1 <- centroids[4:6,c("Group.1","X","Y")]
centroid <- rename(centroid1,c("Group.1"="ID","X"="X-centroid","Y"="Y-centroid"))
data3 <- merge(data1,centroid, by="ID")
head(data3)
## ID X Y isBoundary isLandmark X-centroid Y-centroid
## 1 4 193 1053 1 0 1489.556 930.6111
## 2 4 580 1127 1 0 1489.556 930.6111
## 3 4 1173 1229 1 0 1489.556 930.6111
## 4 4 1619 1285 1 0 1489.556 930.6111
## 5 4 1697 1278 1 0 1489.556 930.6111
## 6 4 2106 1229 1 0 1489.556 930.6111
Calculate the distance from one point to the centroid
a = data.frame()
for (i in 1:length(data3[,1])){
#print(i)
distance <- sqrt((data3[i,2] - data3[i,6])^2 + (data3[i,3] -data3[i,7])^2)
a <- rbind(a, distance)
#print(distance)
}
colnames(a) <- c("Distance")
X <- cbind(data3,a)
d1 <- X[,c("ID","X","Y","X-centroid","Y-centroid","Distance","isBoundary","isLandmark")]
head(d1)
## ID X Y X-centroid Y-centroid Distance isBoundary isLandmark
## 1 4 193 1053 1489.556 930.6111 1302.3192 1 0
## 2 4 580 1127 1489.556 930.6111 930.5159 1 0
## 3 4 1173 1229 1489.556 930.6111 435.0211 1 0
## 4 4 1619 1285 1489.556 930.6111 377.2895 1 0
## 5 4 1697 1278 1489.556 930.6111 404.6137 1 0
## 6 4 2106 1229 1489.556 930.6111 684.8647 1 0
Split data following wings ID
data_split <- split(d1, d1$ID)
data_split
## $`4`
## ID X Y X-centroid Y-centroid Distance isBoundary isLandmark
## 1 4 193 1053 1489.556 930.6111 1302.31922 1 0
## 2 4 580 1127 1489.556 930.6111 930.51593 1 0
## 3 4 1173 1229 1489.556 930.6111 435.02109 1 0
## 4 4 1619 1285 1489.556 930.6111 377.28948 1 0
## 5 4 1697 1278 1489.556 930.6111 404.61369 1 0
## 6 4 2106 1229 1489.556 930.6111 684.86472 1 0
## 7 4 2301 1109 1489.556 930.6111 830.82169 1 1
## 8 4 2378 1025 1489.556 930.6111 893.44434 1 1
## 9 4 2417 924 1489.556 930.6111 927.46801 1 0
## 10 4 2323 832 1489.556 930.6111 839.25788 1 1
## 11 4 2168 720 1489.556 930.6111 710.38293 1 1
## 12 4 1963 620 1489.556 930.6111 566.24103 1 1
## 13 4 1775 558 1489.556 930.6111 469.37999 1 1
## 14 4 1734 550 1489.556 930.6111 452.34711 1 1
## 15 4 1400 527 1489.556 930.6111 413.42729 1 1
## 16 4 618 775 1489.556 930.6111 885.33830 1 1
## 17 4 295 898 1489.556 930.6111 1195.00061 1 0
## 18 4 72 1012 1489.556 930.6111 1419.89010 1 0
## 19 4 510 1021 1489.556 930.6111 983.71705 0 0
## 20 4 520 988 1489.556 930.6111 971.25252 0 0
## 21 4 600 961 1489.556 930.6111 890.07447 0 0
## 22 4 571 918 1489.556 930.6111 918.64212 0 0
## 23 4 584 881 1489.556 930.6111 906.91352 0 0
## 24 4 573 841 1489.556 930.6111 920.92575 0 0
## 25 4 539 838 1489.556 930.6111 955.05638 0 0
## 26 4 547 814 1489.556 930.6111 949.74161 0 0
## 27 4 835 906 1489.556 930.6111 655.01808 0 1
## 28 4 876 941 1489.556 930.6111 613.64350 0 1
## 29 4 878 978 1489.556 930.6111 613.38887 0 1
## 30 4 784 1086 1489.556 930.6111 722.46408 0 1
## 31 4 1224 1041 1489.556 930.6111 287.58557 0 1
## 32 4 1241 845 1489.556 930.6111 262.88615 0 1
## 33 4 1419 575 1489.556 930.6111 362.54289 0 0
## 34 4 1455 701 1489.556 930.6111 232.19679 0 0
## 35 4 1529 935 1489.556 930.6111 39.68786 0 0
## 36 4 1560 939 1489.556 930.6111 70.94218 0 0
## 37 4 1496 1133 1489.556 930.6111 202.49146 0 1
## 38 4 1570 1111 1489.556 930.6111 197.51319 0 0
## 39 4 1736 1201 1489.556 930.6111 365.84835 0 0
## 40 4 1640 1238 1489.556 930.6111 342.23013 0 0
## 41 4 1799 1217 1489.556 930.6111 421.63309 0 0
## 42 4 1814 1207 1489.556 930.6111 426.21006 0 0
## 43 4 2020 1021 1489.556 930.6111 538.09057 0 0
## 44 4 1715 1017 1489.556 930.6111 241.42957 0 0
## 45 4 1998 1051 1489.556 930.6111 522.50286 0 0
## 46 4 2170 931 1489.556 930.6111 680.44456 0 0
## 47 4 2041 726 1489.556 930.6111 588.18082 0 0
## 48 4 1777 845 1489.556 930.6111 299.92261 0 0
## 49 4 2180 836 1489.556 930.6111 696.89654 0 0
## 50 4 2333 929 1489.556 930.6111 843.44598 0 0
## 51 4 2278 1019 1489.556 930.6111 793.38341 0 0
## 52 4 2190 765 1489.556 930.6111 719.75653 0 0
## 53 4 215 1010 1489.556 930.6111 1277.02563 0 0
## 54 4 230 978 1489.556 930.6111 1260.44671 0 0
## 55 4 1558 566 1489.556 930.6111 370.97965 0 0
##
## $`5`
## ID X Y X-centroid Y-centroid Distance isBoundary isLandmark
## 56 5 273 1129 1624.571 1082.81 1352.3605 1 0
## 57 5 576 1203 1624.571 1082.81 1055.4373 1 0
## 58 5 954 1258 1624.571 1082.81 693.0785 1 0
## 59 5 1181 1299 1624.571 1082.81 493.4510 1 0
## 60 5 1539 1371 1624.571 1082.81 300.6264 1 0
## 61 5 1895 1407 1624.571 1082.81 422.1742 1 1
## 62 5 2157 1381 1624.571 1082.81 610.2440 1 0
## 63 5 2258 1340 1624.571 1082.81 683.6510 1 1
## 64 5 2411 1238 1624.571 1082.81 801.5946 1 1
## 65 5 2485 1158 1624.571 1082.81 863.7077 1 1
## 66 5 2515 1070 1624.571 1082.81 890.5207 1 1
## 67 5 2427 992 1624.571 1082.81 807.5506 1 1
## 68 5 2280 898 1624.571 1082.81 680.9854 1 1
## 69 5 2190 859 1624.571 1082.81 608.1120 1 0
## 70 5 2059 808 1624.571 1082.81 514.0510 1 1
## 71 5 1846 736 1624.571 1082.81 411.4699 1 1
## 72 5 1652 710 1624.571 1082.81 373.8172 1 0
## 73 5 1286 771 1624.571 1082.81 460.2780 1 0
## 74 5 813 939 1624.571 1082.81 824.2144 1 0
## 75 5 533 1025 1624.571 1082.81 1093.1012 1 0
## 76 5 786 1147 1624.571 1082.81 841.0246 1 0
## 77 5 713 1109 1624.571 1082.81 911.9476 0 0
## 78 5 792 1082 1624.571 1082.81 832.5718 0 0
## 79 5 756 1041 1624.571 1082.81 869.5771 0 0
## 80 5 784 1014 1624.571 1082.81 843.3831 0 0
## 81 5 776 986 1624.571 1082.81 854.0758 0 0
## 82 5 970 1219 1624.571 1082.81 668.5893 0 1
## 83 5 972 1201 1624.571 1082.81 663.1881 0 0
## 84 5 921 1209 1624.571 1082.81 714.7984 0 0
## 85 5 905 1166 1624.571 1082.81 724.3643 0 0
## 86 5 1116 1107 1624.571 1082.81 509.1464 0 1
## 87 5 1114 1078 1624.571 1082.81 510.5941 0 1
## 88 5 1060 1039 1624.571 1082.81 566.2686 0 1
## 89 5 1281 830 1624.571 1082.81 426.5606 0 0
## 90 5 1431 1164 1624.571 1082.81 209.9090 0 1
## 91 5 1474 1186 1624.571 1082.81 182.5377 0 0
## 92 5 1455 994 1624.571 1082.81 191.4200 0 1
## 93 5 1691 863 1624.571 1082.81 229.6279 0 0
## 94 5 1715 1260 1624.571 1082.81 198.9316 0 1
## 95 5 1891 1377 1624.571 1082.81 396.9033 0 0
## 96 5 1852 1305 1624.571 1082.81 317.9503 0 0
## 97 5 1949 1227 1624.571 1082.81 355.0279 0 0
## 98 5 2159 1328 1624.571 1082.81 587.9900 0 0
## 99 5 2002 916 1624.571 1082.81 412.6472 0 0
## 100 5 2208 1078 1624.571 1082.81 583.4484 0 0
## 101 5 2350 1076 1624.571 1082.81 725.4605 0 0
## 102 5 2172 912 1624.571 1082.81 573.4579 0 0
## 103 5 1930 1323 1624.571 1082.81 388.5590 0 0
##
## $`6`
## ID X Y X-centroid Y-centroid Distance isBoundary isLandmark
## 104 6 238 1037 1365.174 962.6957 1129.62036 1 0
## 105 6 385 1070 1365.174 962.6957 986.02998 1 0
## 106 6 504 1092 1365.174 962.6957 870.82726 1 0
## 107 6 1007 1197 1365.174 962.6957 428.00360 1 0
## 108 6 1249 1244 1365.174 962.6957 304.34933 1 0
## 109 6 1619 1289 1365.174 962.6957 413.40320 1 0
## 110 6 1705 1287 1365.174 962.6957 469.73937 1 1
## 111 6 2067 1238 1365.174 962.6957 753.89146 1 1
## 112 6 2153 1193 1365.174 962.6957 820.79841 1 0
## 113 6 2239 1123 1365.174 962.6957 888.40842 1 1
## 114 6 2309 1037 1365.174 962.6957 946.74644 1 1
## 115 6 2344 955 1365.174 962.6957 978.85634 1 1
## 116 6 2270 859 1365.174 962.6957 910.74861 1 1
## 117 6 2127 769 1365.174 962.6957 786.06424 1 1
## 118 6 1910 683 1365.174 962.6957 612.42561 1 1
## 119 6 1683 607 1365.174 962.6957 477.00400 1 1
## 120 6 1509 591 1365.174 962.6957 398.55188 1 0
## 121 6 1183 626 1365.174 962.6957 382.82019 1 0
## 122 6 907 716 1365.174 962.6957 520.36725 1 0
## 123 6 874 726 1365.174 962.6957 545.23082 1 0
## 124 6 391 869 1365.174 962.6957 978.66935 1 0
## 125 6 189 924 1365.174 962.6957 1176.81028 1 0
## 126 6 537 1010 1365.174 962.6957 829.52380 1 0
## 127 6 545 984 1365.174 962.6957 820.45056 0 0
## 128 6 584 922 1365.174 962.6957 782.23323 0 0
## 129 6 588 906 1365.174 962.6957 779.23917 0 0
## 130 6 610 894 1365.174 962.6957 758.29198 0 0
## 131 6 602 869 1365.174 962.6957 768.90396 0 0
## 132 6 561 863 1365.174 962.6957 810.33012 0 0
## 133 6 565 843 1365.174 962.6957 809.07684 0 0
## 134 6 616 967 1365.174 962.6957 749.18628 0 0
## 135 6 799 1088 1365.174 962.6957 579.87419 0 1
## 136 6 889 992 1365.174 962.6957 477.07477 0 1
## 137 6 893 957 1365.174 962.6957 472.20826 0 1
## 138 6 846 918 1365.174 962.6957 521.09428 0 1
## 139 6 1224 877 1365.174 962.6957 165.14787 0 1
## 140 6 1218 1062 1365.174 962.6957 177.54299 0 1
## 141 6 1322 765 1365.174 962.6957 202.35503 0 0
## 142 6 1200 671 1365.174 962.6957 335.21452 0 0
## 143 6 1431 748 1365.174 962.6957 224.56023 0 0
## 144 6 1312 886 1365.174 962.6957 93.32571 0 0
## 145 6 1269 1092 1365.174 962.6957 161.14911 0 0
## 146 6 1320 1201 1365.174 962.6957 242.54823 0 0
## 147 6 1507 1145 1365.174 962.6957 230.97514 0 1
## 148 6 1597 1242 1365.174 962.6957 362.97969 0 0
## 149 6 1636 1252 1365.174 962.6957 396.28749 0 0
## 150 6 1617 1195 1365.174 962.6957 342.61011 0 0
## 151 6 1683 1211 1365.174 962.6957 403.32180 0 0
## 152 6 1789 1215 1365.174 962.6957 493.24034 0 0
## 153 6 1826 881 1365.174 962.6957 468.01160 0 0
## 154 6 1580 632 1365.174 962.6957 394.34739 0 0
## 155 6 2102 1037 1365.174 962.6957 740.56318 0 0
## 156 6 2096 951 1365.174 962.6957 730.91967 0 0
## 157 6 2176 869 1365.174 962.6957 816.22167 0 0
Function to find intersection point to the boundary
intersection_point = function(a, data, direct){
if ((direct == "North") || (direct == "South")){
slope = (data[2,2] - data[1,2])/(data[[2]]-data[[1]])
c1 = - ((data[2,2] - slope*data[2,1]))
A <- matrix(c(1,slope,0,-1), nrow=2, ncol=2)
b <- matrix(cbind(a[1],c1), nrow=2, ncol=1)
Z1<-optR(A, b, method="gauss")
output1 <- matrix(unlist(Z1[3]), nrow=1 , ncol =2)
} else{
slope = (data[2,2] - data[1,2])/(data[[2]]-data[[1]])
c1 = - ((data[2,2] - slope*data[2,1]))
A <- matrix(c(0,slope,1,-1), nrow=2, ncol=2)
b <- matrix(cbind(a[2],c1), nrow=2, ncol=1)
Z1<-optR(A, b, method="gauss")
output1 <- matrix(unlist(Z1[3]), nrow=1 , ncol =2)
}
return(output1)
}
Function to calculate the distance from one point to the intersection point in the boundary
distanceToBoundary= function(a, b){
distance <- sqrt((b[1]- a[1])^2 + (b[2]-a[2])^2)
return(distance)
}
Function to find the intersection point
find_intersection = function(point,data){
if(((nrow(point) != 1) && (ncol(point) != 2)) || (!is.matrix(data))) {
print("Error! Reinput data.")
}
else{
#X-coordinates
north = matrix(ncol = 2)
north = north[-1,]
south = matrix(ncol = 2)
south = south[-1,]
for (i in 1:(nrow(data)-1)){
if ( (i ==1) && (((data[i,1] < point[1]) && (data[nrow(data),1] > point[1])) || ((data[i,1] > point[1]) && (data[nrow(data),1] < point[1])) )){
north <- rbind(north,data[i,],data[nrow(data),])
rownames(north, do.NULL = FALSE)
rownames(north) <- c("North","")
}
if (((data[i,1] < point[1]) && (data[i+1,1] > point[1])) ){
north <- rbind(north,data[i,],data[i+1,])
rownames(north, do.NULL = FALSE)
rownames(north) <- c("North","")
}
if ( ((data[i,1] > point[1]) && (data[i+1,1] < point[1])) ){
south <- rbind(south,data[i,],data[i+1,])
rownames(south, do.NULL = FALSE)
rownames(south) <- c("South","")
}
}
temp = rbind(north, south)
#y-coordinates
east = matrix(ncol=2)
east = east[-1,]
west = matrix(ncol=2)
west = west[-1,]
for (j in 1:(nrow(data)-1)){
if ( (j ==1) && (((data[j,2] < point[2]) && (data[nrow(data),2] > point[2])) || ((data[j,2] > point[2]) && (data[nrow(data),2] < point[2])) )){
west <- rbind(west,data[j,],data[nrow(data),])
rownames(west, do.NULL = FALSE)
rownames(west) <- c("West","")
}
if (((data[j,2] < point[2]) && (data[j+1,2] > point[2])) ){
west <- rbind(west,data[j,],data[j+1,])
rownames(west, do.NULL = FALSE)
rownames(west) <- c("West","")
}
if (((data[j,2] > point[2]) && (data[j+1,2] < point[2])) ){
east <- rbind(east,data[j,],data[j+1,])
rownames(east, do.NULL = FALSE)
rownames(east) <- c("East","")
}
}
temp1=rbind(east, west)
result= rbind(temp, temp1)
output = matrix(ncol=1, nrow=1)
output = output[-1,]
for (k in 1:(nrow(result))){
if(k%%2 != 0 ){
tempx <- data.matrix(result[k:(k+1),])
inter_point <- intersection_point(point,tempx,row.names(result)[k])
z <- data.matrix(inter_point)
temp5<-distanceToBoundary(point,z )
temp5 = data.matrix(temp5)
rownames(temp5, do.NULL = FALSE)
rownames(temp5) <- c(row.names(result)[k])
output <- rbind(output, temp5 )
}
}
colnames(output, do.NULL = FALSE)
#colnames(output) <- c("Distance")
return(output)
}
}
Reformat the form of output
set_output = function(data){
if(nrow(data) <4){
rw = 4-nrow(data)
g = matrix(0, ncol =1, nrow=rw)
o <-rownames(data, do.NULL = FALSE)
t = c("North", "South", "East", "West")
d <-setdiff(t,o)
rownames(g)<- d
dt <- rbind(g,data)
dt <- dt[match(t,row.names(dt)),1,drop=FALSE]
return(dt)
} else{
return(data)
}
}
Test on a wing
wings <- data.frame(data_split[[1]])
boundary <- split(wings, wings$isBoundary)
temp6 <- data.matrix(boundary[[2]]) #isBoundary is 1
m <- matrix(0, ncol=4, nrow=1)
m <- m[-1,]
colnames(m, do.NULL = FALSE)
## [1] "col1" "col2" "col3" "col4"
colnames(m)<-c("North","South","East","West")
temp7 = data.frame(m)
for (i in 1:nrow(wings)){
z <- data.matrix(wings[i,2:3])
f <- find_intersection(z,temp6[,2:3])
f <- t(set_output(f))
rownames(f)<- NULL
temp7<-rbind(temp7, f)
}
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
## Warning in opt.matrix.reorder(A, tol): Singular Matrix
head(temp7)
## North South East West
## 1 0 102.8565 2159.333 0.00
## 2 0 337.5294 1691.750 0.00
## 3 0 630.0102 0.000 0.00
## 4 0 742.9192 0.000 0.00
## 5 0 730.5479 0.000 133.75
## 6 0 539.2439 0.000 0.00
Random forest Split to 70% Training and 30% Testing data
d <-cbind(wings, temp7)
ind <- sample(2,nrow(d),replace=TRUE,prob=c(0.7,0.3))
trainData <- d[ind==1,]
testData <- d[ind==2,]
head(d)
## ID X Y X.centroid Y.centroid Distance isBoundary isLandmark North
## 1 4 193 1053 1489.556 930.6111 1302.3192 1 0 0
## 2 4 580 1127 1489.556 930.6111 930.5159 1 0 0
## 3 4 1173 1229 1489.556 930.6111 435.0211 1 0 0
## 4 4 1619 1285 1489.556 930.6111 377.2895 1 0 0
## 5 4 1697 1278 1489.556 930.6111 404.6137 1 0 0
## 6 4 2106 1229 1489.556 930.6111 684.8647 1 0 0
## South East West
## 1 102.8565 2159.333 0.00
## 2 337.5294 1691.750 0.00
## 3 630.0102 0.000 0.00
## 4 742.9192 0.000 0.00
## 5 730.5479 0.000 133.75
## 6 539.2439 0.000 0.00
Generate Random Forest learning tree
d_rf <- randomForest(isLandmark~., data = trainData, ntree = 100, promixity=TRUE )
## Warning in randomForest.default(m, y, ...): The response has five or fewer
## unique values. Are you sure you want to do regression?
table(predict(d_rf),trainData$isLandmark)
##
## 0 1
## 0.0557523284796012 1 0
## 0.0761904761904761 1 0
## 0.0826254826254826 1 0
## 0.0986111111111111 1 0
## 0.0995918367346938 1 0
## 0.103785103785104 1 0
## 0.105092592592593 1 0
## 0.122023809523809 1 0
## 0.127324263038549 1 0
## 0.135714285714286 1 0
## 0.138172043010753 1 0
## 0.146875 1 0
## 0.152941176470588 0 1
## 0.167647058823529 1 0
## 0.178787878787879 1 0
## 0.195702671312427 1 0
## 0.202222222222222 1 0
## 0.214285714285714 1 0
## 0.221621621621622 1 0
## 0.235023041474654 1 0
## 0.235294117647059 1 0
## 0.235416666666667 1 0
## 0.273404255319149 1 0
## 0.338235294117647 1 0
## 0.338541666666667 1 0
## 0.349593495934959 0 1
## 0.385064935064935 0 1
## 0.413880742913001 1 0
## 0.424242424242424 0 1
## 0.441919191919192 1 0
## 0.496190476190476 0 1
## 0.541472868217054 0 1
## 0.607456140350877 0 1
## 0.696969696969697 0 1
## 0.700196772924045 0 1
## 0.701219512195122 0 1
## 0.71875 0 1
## 0.718843537414966 0 1
Print Random Forest to see the importance features
print(d_rf)
##
## Call:
## randomForest(formula = isLandmark ~ ., data = trainData, ntree = 100, promixity = TRUE)
## Type of random forest: regression
## Number of trees: 100
## No. of variables tried at each split: 3
##
## Mean of squared residuals: 0.1083033
## % Var explained: 49.88
plot(d_rf)
importance(d_rf)
## IncNodePurity
## ID 0.0000000
## X 1.0968152
## Y 0.7333417
## X.centroid 0.0000000
## Y.centroid 0.0000000
## Distance 0.8660220
## isBoundary 0.5997057
## North 0.5848447
## South 1.3004015
## East 1.4925165
## West 0.8116926
varImpPlot(d_rf)
Try to build random forest for testing data
dPred <- predict(d_rf, newdata = testData)
table(dPred, testData$isLandmark)
##
## dPred 0 1
## 0.0670714285714286 1 0
## 0.0739047619047619 1 0
## 0.0757316017316017 1 0
## 0.0798268398268398 1 0
## 0.0849047619047619 1 0
## 0.0928333333333333 1 0
## 0.119904761904762 0 1
## 0.197523809523809 1 0
## 0.227333333333333 1 0
## 0.228309523809524 1 0
## 0.342595238095238 1 0
## 0.347136363636364 1 0
## 0.359309523809524 1 0
## 0.412564935064935 0 1
## 0.694166666666667 0 1
## 0.774595238095238 1 0
## 0.778833333333333 0 1
Try to see the margin, positive or negative, if positif it means correct classification
#plot(margin(d_rf,testData$isLandmark))# margin not defined for regression Random Forests
Tune Random Forest
# tune.rf <- tuneRF(d[,-5],d[,5], stepFactor = 0.5)
# print(tune.rf)