Saturday, 13 April 2019

PCA


Dataset with missing names, and you want to quickly identify the relation between variables, identify the 'Principle Components'.
>library(ggplot2)
>data(mpg)
>data <- mpg[,c("displ", "year", "cyl", "cty", "hwy")]
# get the numeric columns only for this easy demo
>prcomp(data, scale=TRUE)

Standard deviations:
  [1] 1.8758132 1.0069712 0.5971261 0.2658375 0.2002613

Rotation:
  PC1         PC2        PC3         PC4         PC5
displ  0.49818034 -0.07540283  0.4897111  0.70386376 -0.10435326
year   0.06047629 -0.98055060 -0.1846807 -0.01604536  0.02233245
cyl    0.49820578 -0.04868461  0.5028416 -0.68062021  0.18255766
cty   -0.50575849 -0.09911736  0.4348234  0.15195854  0.72264881
hwy   -0.49412379 -0.14366800  0.5330619 -0.13410105 -0.65807527

Here is how you interpret the result:

(1) The standard deviations, which is the diagonal matrix in the middle when you apply the singular value decomposition. Explains how much variance each 'Principle Component'? / layer / transparency explains in the whole variance in the matrix. For example,
70 % = 1.8758132^2 / (1.8758132^2 + 1.0069712^2 + 0.5971261^2 + 0.2658375^2 + 0.2002613^2)
Which indicates the first column itself already explains 70% of the variance in the whole matrix.
(2) Now let's look at the first column in the rotation matrix / V:
          PC1      
displ  0.49818034
year   0.06047629
cyl    0.49820578
cty   -0.50575849
hwy   -0.49412379

We can see: displ has a positive relation with cyl and negative relation with cty and hwy. And in this dominant layer, year is not that obvious.

The makes sense, the more displacement or cylinders you have in your car, it probably has a very high MPG.
Here is the plot between the variables just for you information.
pairs(data)

density


d <- density(mtcars$mpg)
 
plot(d)

# Filled Density Plot
d <- density(mtcars$mpg)
plot(d, main="Kernel Density of Miles Per Gallon")
polygon(d, col="red", border="blue")

Box New

input=mtcars[,c('mpg','cyl')]    -  notch=false is box format and true is like some different format    
print(tail(input))

boxplot(mpg ~ cyl,data=mtcars,xlab="number of cylinders",ylab="miles per gallon",main="mileage data",notch=FALSE,varwidth=TRUE,col=c("green","yellow","purple"),names=c("high","medium","low"))

BoxPlot




input=mtcars[16:20,c('mpg','cyl')]         
 boxplot(mpg ~ cyl,data=input,xlab="number of cylinders",ylab="miles per gallon",main="mileage data")

Bar Plot


h=c(7,12,28,3,41)                 
m=c("mar","apr","may","jun","jul")
barplot(h,names.arg=m,xlab="month",ylab="revenue",col="blue",main="revenue chart",border="red")

___________________________________________________
colors=c("green","orange","brown")   
months=c("mar","apr","may","jun","jul")
regions=c("east","west","north")

values=matrix(c(2,4,5,3,5,2,5,3,7,8,9,4,7),nrow=3,ncol=5,byrow=TRUE)
barplot(values,names.arg=m,xlab="month",ylab="revenue",col=colors,main="revenue chart",border="red")
legend("topleft",regions,cex=1.3,fill=colors)

PIE Plot


x=c(21,62,10,53)
labels=c("london","newyork","singapore","Dubai")
piepercent=round(100*x/sum(x),1)
pie(x,labels,col=rainbow(length(x)),radius=1.0,main="popoular cities",clockwise = FALSE)
legend("topright",c("london","newyork","singapore","mumbai"), cex=0.8, fill=rainbow(length(x)))

Pie

x=c(21,62,10,53)
labels=c("london","newyork","singapore","Dubai")
pie(x,labels)

ANN


install.packages('neuralnet')
library("neuralnet")

#Going to create a neural network to perform sqare rooting
#Type ?neuralnet for more information on the neuralnet library

#Generate 50 random numbers uniformly distributed between 0 and 100
#And store them as a dataframe
traininginput <-  as.data.frame(runif(50, min=0, max=100))
trainingoutput <- sqrt(traininginput)

#Column bind the data into one variable
trainingdata <- cbind(traininginput,trainingoutput)
colnames(trainingdata) <- c("Input","Output")

#Train the neural network
#Going to have 10 hidden layers
#Threshold is a numeric value specifying the threshold for the partial
#derivatives of the error function as stopping criteria.
net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=10, threshold=0.01)
print(net.sqrt)

#Plot the neural network
plot(net.sqrt)

#Test the neural network on some training data
testdata <- as.data.frame((1:10)^2) #Generate some squared numbers
net.results <- compute(net.sqrt, testdata) #Run them through the neural network

#Lets see what properties net.sqrt has
ls(net.results)

#Lets see the results
print(net.results$net.result)

#Lets display a better version of the results
cleanoutput <- cbind(testdata,sqrt(testdata),
                         as.data.frame(net.results$net.result))
colnames(cleanoutput) <- c("Input","Expected Output","Neural Net Output")
print(cleanoutput)