# Skript na vypocet vzdalenosti pomoci ruznych metrik vzdalenosti. Obsahuje i vizualizace vypoctu vzdalenosti. # by Eva Koritakova (Janousova), 26/oct/2015. # -----------------------------------------------------------------------------------------------------------# # data: X1=matrix(c(4,3,10,8),2,2) X1 x0=as.matrix(c(3.5,9)) # vykresleni dat: plot(X1,xlim=c(2,5),ylim=c(7,11),col="red",pch=16,cex=1.5,asp=1,xlab="p1",ylab="p2") # asp=1 - x and y ranges are same points(t(x0),pch=16,col="blue",cex=1.5) # na zaklade tohoto obrazku by se dalo usoudit, ze ma modry bod x0 stejnou vzdalenost od cervenych bodu -> ne ale vzdy! (pri pouziti ruznych metrik to muze byt ruzne, coz si ukazeme) #### Euklidova metrika - vypocet vzdalenosti subjektu x0 od dvou ostatnich subjektu: euc.dist <- array(0,nrow(X1)) for (i in 1:nrow(X1)) { euc.dist[i] <- sqrt(sum((X1[i,] - x0) ^ 2)) # vypocet vzdalenosti } euc.dist ## Euklidova metrika - vykresleni: windows(10,5) par(mfrow=c(1,2),mar=c(5,5,1,1),oma=c(0,0,0,0)) # vzdalenosti subjektu x0 od dvou ostatnich subjektu plot(X1,xlim=c(2,5),ylim=c(7,11),col="red",pch=16,cex=1.5,asp=1,xlab="p1",ylab="p2") # asp=1 - x and y ranges are same points(t(x0),pch=16,col="blue",cex=1.5) lines(c(x0[1],X1[1,1]),c(x0[2],X1[1,2]),col="blue",lwd=2) lines(c(x0[1],X1[2,1]),c(x0[2],X1[2,2]),col="blue",lwd=2) # znazorneni bodu, ktere lezi ve stejne vzdalenosti od bodu x0 plot(X1,xlim=c(2,5),ylim=c(7,11),col="red",pch=16,cex=1.5,asp=1,xlab="p1",ylab="p2") # asp=1 - x and y ranges are same points(t(x0),pch=16,col="blue",cex=1.5) #install.packages("plotrix") library(plotrix) draw.circle(x0[1],x0[2],min(euc.dist),border="blue",lty=1,lwd=2) #### Hammingova (manhattanska) metrika - vypocet vzdalenosti subjektu x0 od dvou ostatnich subjektu: h.dist <- array(0,nrow(X1)) for (i in 1:nrow(X1)) { h.dist[i] <- sum(abs(X1[i,]-x0)) # vypocet vzdalenosti } h.dist ## Hammingova (manhattanska) metrika - vykresleni: windows(10,5) par(mfrow=c(1,2),mar=c(5,5,1,1),oma=c(0,0,0,0)) # vzdalenosti subjektu x0 od dvou ostatnich subjektu plot(X1,xlim=c(2,5),ylim=c(7,11),col="red",pch=16,cex=1.5,asp=1,xlab="p1",ylab="p2") # asp=1 - x and y ranges are same points(t(x0),pch=16,col="blue",cex=1.5) lines(c(x0[1],X1[1,1]),c(x0[2],x0[2]),col="blue",lwd=2) lines(c(X1[1,1],X1[1,1]),c(x0[2],X1[1,2]),col="blue",lwd=2) lines(c(x0[1],X1[2,1]),c(x0[2],x0[2]),col="blue",lwd=2) lines(c(X1[2,1],X1[2,1]),c(x0[2],X1[2,2]),col="blue",lwd=2) # znazorneni bodu, ktere lezi ve stejne vzdalenosti od bodu x0 plot(X1,xlim=c(2,5),ylim=c(7,11),col="red",pch=16,cex=1.5,asp=1,xlab="p1",ylab="p2") # asp=1 - x and y ranges are same points(t(x0),pch=16,col="blue",cex=1.5) lines(c(x0[1]-min(h.dist),x0[1]),c(x0[2],x0[2]-min(h.dist)),col="blue",lwd=2) lines(c(x0[1]-min(h.dist),x0[1]),c(x0[2],x0[2]+min(h.dist)),col="blue",lwd=2) lines(c(x0[1],x0[1]+min(h.dist)),c(x0[2]-min(h.dist),x0[2]),col="blue",lwd=2) lines(c(x0[1],x0[1]+min(h.dist)),c(x0[2]+min(h.dist),x0[2]),col="blue",lwd=2) #### Cebysevova metrika - vypocet vzdalenosti subjektu x0 od dvou ostatnich subjektu: ceb.dist <- array(0,nrow(X1)) for (i in 1:nrow(X1)) { ceb.dist[i] = max(abs(X1[i,]-x0)) # vypocet vzdalenosti } ceb.dist ## Cebysevova metrika - vykresleni: windows(10,5) par(mfrow=c(1,2),mar=c(5,5,1,1),oma=c(0,0,0,0)) # vzdalenosti subjektu x0 od dvou ostatnich subjektu plot(X1,xlim=c(2,5),ylim=c(7,11),col="red",pch=16,cex=1.5,asp=1,xlab="p1",ylab="p2") # asp=1 - x and y ranges are same points(t(x0),pch=16,col="blue",cex=1.5) lines(c(x0[1],X1[1,1]),c(x0[2],x0[2]),col="gray",lwd=2) lines(c(X1[1,1],X1[1,1]),c(x0[2],X1[1,2]),col="blue",lwd=2) lines(c(x0[1],X1[2,1]),c(x0[2],x0[2]),col="gray",lwd=2) lines(c(X1[2,1],X1[2,1]),c(x0[2],X1[2,2]),col="blue",lwd=2) # znazorneni bodu, ktere lezi ve stejne vzdalenosti od bodu x0 plot(X1,xlim=c(2,5),ylim=c(7,11),col="red",pch=16,cex=1.5,asp=1,xlab="p1",ylab="p2") # asp=1 - x and y ranges are same points(t(x0),pch=16,col="blue",cex=1.5) lines(c(x0[1]-min(ceb.dist),x0[1]-min(ceb.dist)),c(x0[2]-min(ceb.dist),x0[2]+min(ceb.dist)),col="blue",lwd=2) lines(c(x0[1]-min(ceb.dist),x0[1]+min(ceb.dist)),c(x0[2]-min(ceb.dist),x0[2]-min(ceb.dist)),col="blue",lwd=2) lines(c(x0[1]-min(ceb.dist),x0[1]+min(ceb.dist)),c(x0[2]+min(ceb.dist),x0[2]+min(ceb.dist)),col="blue",lwd=2) lines(c(x0[1]+min(ceb.dist),x0[1]+min(ceb.dist)),c(x0[2]-min(ceb.dist),x0[2]+min(ceb.dist)),col="blue",lwd=2) ##### Vykresleni do jednoho grafu: windows(10,5) par(mfrow=c(1,2),mar=c(5,5,1,1),oma=c(0,0,0,0)) # vzdalenosti subjektu x0 od dvou ostatnich subjektu plot(X1,xlim=c(2,5),ylim=c(7,11),col="red",pch=16,cex=1.5,asp=1,xlab="p1",ylab="p2") # asp=1 - x and y ranges are same points(t(x0),pch=16,col="blue",cex=1.5) lines(c(x0[1],X1[1,1]),c(x0[2],X1[1,2]),col="green",lwd=2) # Euklidova vzd lines(c(x0[1],X1[2,1]),c(x0[2],X1[2,2]),col="green",lwd=2) # Euklidova vzd lines(c(x0[1],X1[1,1]),c(x0[2],x0[2]),col="gray",lwd=2) lines(c(X1[1,1],X1[1,1]),c(x0[2],X1[1,2]),col="blue",lwd=2) lines(c(x0[1],X1[2,1]),c(x0[2],x0[2]),col="gray",lwd=2) lines(c(X1[2,1],X1[2,1]),c(x0[2],X1[2,2]),col="blue",lwd=2) # znazorneni bodu, ktere lezi ve stejne vzdalenosti od bodu x0 plot(X1,xlim=c(2,5),ylim=c(7,11),col="red",pch=16,cex=1.5,asp=1,xlab="p1",ylab="p2") # asp=1 - x and y ranges are same points(t(x0),pch=16,col="blue",cex=1.5) draw.circle(x0[1],x0[2],min(euc.dist),border="green",lty=1,lwd=2) # Euklidova vzd lines(c(x0[1]-min(h.dist),x0[1]),c(x0[2],x0[2]-min(h.dist)),col="magenta",lwd=2) # Hammingova (manhattanska) vzd lines(c(x0[1]-min(h.dist),x0[1]),c(x0[2],x0[2]+min(h.dist)),col="magenta",lwd=2) # Hammingova (manhattanska) vzd lines(c(x0[1],x0[1]+min(h.dist)),c(x0[2]-min(h.dist),x0[2]),col="magenta",lwd=2) # Hammingova (manhattanska) vzd lines(c(x0[1],x0[1]+min(h.dist)),c(x0[2]+min(h.dist),x0[2]),col="magenta",lwd=2) # Hammingova (manhattanska) vzd lines(c(x0[1]-min(ceb.dist),x0[1]-min(ceb.dist)),c(x0[2]-min(ceb.dist),x0[2]+min(ceb.dist)),col="blue",lwd=2) # Cebysevova vzd lines(c(x0[1]-min(ceb.dist),x0[1]+min(ceb.dist)),c(x0[2]-min(ceb.dist),x0[2]-min(ceb.dist)),col="blue",lwd=2) # Cebysevova vzd lines(c(x0[1]-min(ceb.dist),x0[1]+min(ceb.dist)),c(x0[2]+min(ceb.dist),x0[2]+min(ceb.dist)),col="blue",lwd=2) # Cebysevova vzd lines(c(x0[1]+min(ceb.dist),x0[1]+min(ceb.dist)),c(x0[2]-min(ceb.dist),x0[2]+min(ceb.dist)),col="blue",lwd=2) # Cebysevova vzd #### Canberrska metrika - vypocet vzdalenosti subjektu x0 od dvou ostatnich subjektu: can.dist <- array(0,nrow(X1)) for (i in 1:nrow(X1)) { can.dist[i] <- sum(abs(X1[i,]-x0)/(abs(X1[i,])+abs(x0))) # vypocet vzdalenosti } can.dist ## Canberrska metrika - vykresleni: x_grid=seq(x0[1,1]-3,x0[1,1]+3,by=0.01) y_grid=seq(x0[2,1]-3,x0[2,1]+3,by=0.01) dist_grid=matrix(0,length(x_grid),length(y_grid)) windows(10,10) plot(x0[1,1],x0[2,1],pch=16,col="blue",cex=1.5,xlim=c(x0[1,1]-3,x0[1,1]+3),asp=1) # asp=1 - x and y ranges are same d=0.12 for (i in 1:length(x_grid)) { for (j in 1:length(y_grid)) { dist_grid[i,j]=(abs(x_grid[i]-x0[1,1])/(abs(x_grid[i])+abs(x0[1,1]))) + (abs(y_grid[j]-x0[2,1])/(abs(y_grid[j])+abs(x0[2,1]))) if (dist_grid[i,j]d-0.001) points(x_grid[i],y_grid[j],pch=16) } } points(t(x0),pch=16,col="blue",cex=1.5) points(X1[1,1],X1[1,2],pch=16,col="red",cex=1.5) points(X1[2,1],X1[2,2],pch=16,col="red",cex=1.5) d2=1.5 # prikresleni Hammingovy (manhattanske) vzdalenosti lines(c(x0[1]-d2,x0[1]),c(x0[2],x0[2]-d2),col="blue",lwd=3) lines(c(x0[1]-d2,x0[1]),c(x0[2],x0[2]+d2),col="blue",lwd=3) lines(c(x0[1],x0[1]+d2),c(x0[2]-d2,x0[2]),col="blue",lwd=3) lines(c(x0[1],x0[1]+d2),c(x0[2]+d2,x0[2]),col="blue",lwd=3)