library(readxl)

#get current directory path
getwd()
## [1] "D:/APGCBAA/III/RProg/workspace/HMV"
#************************************************************************
# hmv SquareFeet and MarketValues Dataset
#************************************************************************
hmv <- read_excel("HomeMarketValue.xlsx",sheet = 1)

# Mean of both variables in all data frames
apply(hmv,2,mean)
##    HouseAge  SquareFeet MarketValue 
##    29.83333  1695.26190 92069.04762
# standard deviation of both variables in all data frames
apply(hmv,2,sd)
##     HouseAge   SquareFeet  MarketValue 
##     2.428657   220.256730 10553.082733
summary(hmv)
##     HouseAge       SquareFeet    MarketValue    
##  Min.   :27.00   Min.   :1468   Min.   : 76600  
##  1st Qu.:28.00   1st Qu.:1520   1st Qu.: 86575  
##  Median :28.00   Median :1666   Median : 88500  
##  Mean   :29.83   Mean   :1695   Mean   : 92069  
##  3rd Qu.:32.00   3rd Qu.:1807   3rd Qu.: 96525  
##  Max.   :33.00   Max.   :2372   Max.   :120700
#Correlation or linear dependence between two variables in all data frames
cor(hmv$SquareFeet,hmv$MarketValue)
## [1] 0.7312552
# Scatter Plot To Visualise The Relationship between SquareFeet and MarketValues
# for all data frames
scatter.smooth(x=hmv$SquareFeet, y=hmv$MarketValue, main="SquareFeet ~ MarketValue", col = "red")

attach(hmv)

#Scatter plot of car mileage versus weight, with superimposed linear and lowess fit lines.
plot(SquareFeet, MarketValue,
     main="Basic Scatter plot of MarketValue vs. SquareFeet",
     xlab="Square Feet",
     ylab="Market Value ", pch=19)
abline(lm(MarketValue~SquareFeet), col="red", lwd=2, lty=1)
lines(lowess(SquareFeet,MarketValue), col="blue", lwd=2, lty=2)

library(car)
## Loading required package: carData
scatterplot(MarketValue ~ SquareFeet | HouseAge, data=hmv, lwd=2,
main="Scatter Plot of MarketValue vs. SquareFeet by # HouseAge",
xlab="Square Feet",
ylab="Market Value", 
boxplots="xy"
)

#Scatter plot matrix created by the pairs() function
pairs(MarketValue~SquareFeet+HouseAge, data=hmv, main="Basic Scatter Plot Matrix")

#Scatterplot Matrix via car pacakage
library(car)


# Scatter plot matrix produced by the scatterplot.Matrix()
# function. The graph includes histograms in the principal diagonal and linear
# and loess fit lines. Additionally, subgroups (defined by number of cylinders)
# are indicated by symbol type and color.
#scatterplotMatrix(~ SquareFeet + HouseAge + MarketValue, data=hmv)

scatterplotMatrix(~ SquareFeet + MarketValue | HouseAge, data=hmv, diagonal="histogram",
main="Scatter Plot Matrix via Home Value Package")
## Warning in applyDefaults(diagonal, defaults = list(method =
## "adaptiveDensity"), : unnamed diag arguments, will be ignored

cor(hmv[c("MarketValue", "SquareFeet", "HouseAge")])
##             MarketValue SquareFeet  HouseAge
## MarketValue   1.0000000  0.7312552 0.3614153
## SquareFeet    0.7312552  1.0000000 0.6456685
## HouseAge      0.3614153  0.6456685 1.0000000
# 3-D Scatterplots
library(scatterplot3d)
attach(hmv)
## The following objects are masked from hmv (pos = 6):
## 
##     HouseAge, MarketValue, SquareFeet
scatterplot3d(MarketValue, HouseAge, SquareFeet,
              main="Basic 3D Scatter Plot")

scatterplot3d(SquareFeet, HouseAge, MarketValue,
              pch=16,
              highlight.3d=TRUE,
              type="h",
              main="3D Scatter Plot with Vertical Lines")

s3d <-scatterplot3d(SquareFeet, HouseAge, MarketValue,
                    pch=16,
                    highlight.3d=TRUE,
                    type="h",
                    main="3D Scatter Plot with Vertical Lines and Regression Plane")
fit <- lm(MarketValue ~ SquareFeet+HouseAge)
s3d$plane3d(fit)
detach(hmv)

# spinning 3D plot
library(rgl)
attach(hmv)
## The following objects are masked from hmv (pos = 7):
## 
##     HouseAge, MarketValue, SquareFeet
plot3d(SquareFeet, HouseAge, MarketValue, col="red", size=5)

# alternative
library(car)
with(hmv,
     scatter3d(SquareFeet, HouseAge, MarketValue))
## Loading required namespace: mgcv

# bubble plots
attach(hmv)
## The following objects are masked from hmv (pos = 3):
## 
##     HouseAge, MarketValue, SquareFeet
## The following objects are masked from hmv (pos = 8):
## 
##     HouseAge, MarketValue, SquareFeet
r <- sqrt(HouseAge/pi)
symbols(SquareFeet, MarketValue, circle=r, inches=0.30,
        fg="white", bg="lightblue",
        main="Bubble Plot with point size proportional to displacement",
        ylab="Market value in Dollars",
        xlab="Area Sq. Ft.")
text(SquareFeet, MarketValue, rownames(hmv), cex=0.6)

detach(hmv)

                         


# Correlograms
options(digits=2)
cor(hmv)
##             HouseAge SquareFeet MarketValue
## HouseAge        1.00       0.65        0.36
## SquareFeet      0.65       1.00        0.73
## MarketValue     0.36       0.73        1.00
library(corrgram)
corrgram(hmv, order=TRUE, lower.panel=panel.shade,
         upper.panel=panel.pie, text.panel=panel.txt,
         main="Corrgram of HMV intercorrelations")

corrgram(hmv, order=TRUE, lower.panel=panel.ellipse,
         upper.panel=panel.pts, text.panel=panel.txt,
         diag.panel=panel.minmax,
         main="Corrgram of HMV data using scatter plots and ellipses")

cols <- colorRampPalette(c("darkgoldenrod4", "burlywood1",
                           "darkkhaki", "darkgreen"))
corrgram(hmv, order=TRUE, col.regions=cols,
         lower.panel=panel.shade,
         upper.panel=panel.conf, text.panel=panel.txt,
         main="A Corrgram (or Horse) of a Different Color")

library(vcd)
## Loading required package: grid
mosaic(~SquareFeet + MarketValue + HouseAge, data=hmv, shade=TRUE, legend=TRUE)

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.