Setup

library(R.matlab)
## Warning: package 'R.matlab' was built under R version 4.0.3
## R.matlab v3.6.2 (2018-09-26) successfully loaded. See ?R.matlab for help.
## 
## Attaching package: 'R.matlab'
## The following objects are masked from 'package:base':
## 
##     getOption, isOpen
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.3
library(ggExtra)
## Warning: package 'ggExtra' was built under R version 4.0.3
library(GGally)
## Warning: package 'GGally' was built under R version 4.0.3
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.0.3
library(rgl)
## Warning: package 'rgl' was built under R version 4.0.3
knitr::knit_hooks$set(webgl = hook_webgl)
library(fmsb)
## Warning: package 'fmsb' was built under R version 4.0.3
library(aplpack)
## Warning: package 'aplpack' was built under R version 4.0.3
library(TeachingDemos)
## Warning: package 'TeachingDemos' was built under R version 4.0.3
## 
## Attaching package: 'TeachingDemos'
## The following objects are masked from 'package:aplpack':
## 
##     faces, slider

Example 1 and Example 2

dollar_sales<-c(42,52,48,58)

number_books <-c(4,5,4,3)

Bookstore_sale<-cbind(dollar_sales, number_books)

colMeans(Bookstore_sale)
## dollar_sales number_books 
##           50            4
cov(Bookstore_sale)
##              dollar_sales number_books
## dollar_sales     45.33333   -2.0000000
## number_books     -2.00000    0.6666667
cor(Bookstore_sale)
##              dollar_sales number_books
## dollar_sales    1.0000000   -0.3638034
## number_books   -0.3638034    1.0000000

Upload Datasets of Examples 1.4, 1.5, 1.6, 1.9, 1.10, 1.11

Bear_data<-readMat("Bear.mat")
## Warning in readMat("Bear.mat"): strings not representable in native encoding
## will be translated to UTF-8
attributes(Bear_data)
## $names
## [1] "Bear"
## 
## $header
## $header$description
## [1] "MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Thu Jan 17 13:03:48 2008                                                  "
## 
## $header$version
## [1] "5"
## 
## $header$endian
## [1] "little"
Bear<-Bear_data$Bear

Bear
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,]    1   48   59   95   82  141  157  168  183
## [2,]    2   59   68  102  102  140  168  174  170
## [3,]    3   61   77   93  107  145  162  172  177
## [4,]    4   54   43  104  104  146  159  176  171
## [5,]    5  100  145  185  247  150  158  168  175
## [6,]    6   68   82   95  118  142  140  178  189
## [7,]    7   68   95  109  111  139  171  176  175
write.csv(Bear, file="Bear.csv") # save the data as csv format in the working directory 
Lizard.size<-readMat("Lizard_Size.mat")

attributes(Lizard.size)
## $names
## [1] "Data.all"
## 
## $header
## $header$description
## [1] "MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Mon Jan 14 10:39:42 2008                                                  "
## 
## $header$version
## [1] "5"
## 
## $header$endian
## [1] "little"
Lizard.size<-as.data.frame(Lizard.size$Data.all)

Lizard.size
##    V1     V2   V3    V4 V5
## 1   1  5.526 59.0 113.5  0
## 2   2 10.401 75.0 142.0  1
## 3   3  9.213 69.0 124.0  0
## 4   4  8.953 67.5 124.0  0
## 5   5  7.063 62.0 129.5  1
## 6   6  6.610 62.0 123.0  0
## 7   7 11.273 74.0 140.0  1
## 8   8  2.447 47.0  97.0  0
## 9   9 15.493 86.5 162.0  1
## 10 10  9.004 69.0 126.5  0
## 11 11  8.199 70.5 136.0  1
## 12 12  6.601 64.5 116.0  0
## 13 13  7.622 67.5 135.0  1
## 14 14 10.067 73.0 136.5  1
## 15 15 10.091 73.0 135.5  1
## 16 16 10.999 77.0 139.0  1
## 17 17  7.610 61.5 118.0  0
## 18 18  7.733 66.5 133.5  1
## 19 19 12.015 79.5 150.0  1
## 20 20 10.049 74.0 137.0  1
## 21 21  5.149 59.5 116.0  0
## 22 22  9.158 68.0 123.0  0
## 23 23 12.132 75.0 141.0  1
## 24 24  6.978 66.5 117.0  0
## 25 25  6.890 63.0 117.0  0
write.csv(Lizard.size, file="Lizard_size.csv") # save the data as csv format in the working directory 
playroll<-read.table("playroll.DAT")

playroll 
##        V1    V2
## 1 3497900 0.623
## 2 2485475 0.593
## 3 1782875 0.512
## 4 1725450 0.500
## 5 1645575 0.463
## 6 1469800 0.395
lumber <- read.table("lumber.DAT")

lumber
##      V1   V2   V3   V4
## 1  1889 1651 1561 1778
## 2  2403 2048 2087 2197
## 3  2119 1700 1815 2222
## 4  1645 1627 1110 1533
## 5  1976 1916 1614 1883
## 6  1712 1712 1439 1546
## 7  1943 1685 1271 1671
## 8  2104 1820 1717 1874
## 9  2983 2794 2412 2581
## 10 1745 1600 1384 1508
## 11 1710 1591 1518 1667
## 12 2046 1907 1627 1898
## 13 1840 1841 1595 1741
## 14 1867 1685 1493 1678
## 15 1859 1649 1389 1714
## 16 1954 2149 1180 1281
## 17 1325 1170 1002 1176
## 18 1419 1371 1252 1308
## 19 1828 1634 1602 1755
## 20 1725 1594 1313 1646
## 21 2276 2189 1547 2111
## 22 1899 1614 1422 1477
## 23 1633 1513 1290 1516
## 24 2061 1867 1646 2037
## 25 1856 1493 1356 1533
## 26 1727 1412 1238 1469
## 27 2168 1896 1701 1834
## 28 1655 1675 1414 1597
## 29 2326 2301 2065 2234
## 30 1490 1382 1214 1284
paper_quality <- read.table("paper_quality.DAT")

paper_quality 
##       V1     V2    V3
## 1  0.801 121.41 70.42
## 2  0.824 127.70 72.47
## 3  0.841 129.20 78.20
## 4  0.816 131.80 74.89
## 5  0.840 135.10 71.21
## 6  0.842 131.50 78.39
## 7  0.820 126.70 69.02
## 8  0.802 115.10 73.10
## 9  0.828 130.80 79.28
## 10 0.819 124.60 76.48
## 11 0.826 118.31 70.25
## 12 0.802 114.20 72.88
## 13 0.810 120.30 68.23
## 14 0.802 115.70 68.12
## 15 0.832 117.51 71.62
## 16 0.796 109.81 53.10
## 17 0.759 109.10 50.85
## 18 0.770 115.10 51.68
## 19 0.759 118.31 50.60
## 20 0.772 112.60 53.51
## 21 0.806 116.20 56.53
## 22 0.803 118.00 70.70
## 23 0.845 131.00 74.35
## 24 0.822 125.70 68.29
## 25 0.971 126.10 72.10
## 26 0.816 125.80 70.64
## 27 0.836 125.50 76.33
## 28 0.815 127.80 76.75
## 29 0.822 130.50 80.33
## 30 0.822 127.90 75.68
## 31 0.843 123.90 78.54
## 32 0.824 124.10 71.91
## 33 0.788 120.80 68.22
## 34 0.782 107.40 54.42
## 35 0.795 120.70 70.41
## 36 0.805 121.91 73.68
## 37 0.836 122.31 74.93
## 38 0.788 110.60 53.52
## 39 0.772 103.51 48.93
## 40 0.776 110.71 53.67
## 41 0.758 113.80 52.42
Utility <- read.table("Utility.txt")

Utility 
##      V1   V2  V3   V4   V5    V6   V7    V8
## 1  1.06  9.2 151 54.4  1.6  9077  0.0 0.628
## 2  0.89 10.3 202 57.9  2.2  5088 25.3 1.555
## 3  1.43 15.4 113 53.0  3.4  9212  0.0 1.058
## 4  1.02 11.2 168 56.0  0.3  6423 34.3 0.700
## 5  1.49  8.8 192 51.2  1.0  3300 15.6 2.044
## 6  1.32 13.5 111 60.0 -2.2 11127 22.5 1.241
## 7  1.22 12.2 175 67.6  2.2  7642  0.0 1.652
## 8  1.10  9.2 245 57.0  3.3 13082  0.0 0.309
## 9  1.34 13.0 168 60.4  7.2  8406  0.0 0.862
## 10 1.12 12.4 197 53.0  2.7  6455 39.2 0.623
## 11 0.75  7.5 173 51.5  6.5 17441  0.0 0.768
## 12 1.13 10.9 178 62.0  3.7  6154  0.0 1.897
## 13 1.15 12.7 199 53.7  6.4  7179 50.2 0.527
## 14 1.09 12.0  96 49.8  1.4  9673  0.0 0.588
## 15 0.96  7.6 164 62.2 -0.1  6468  0.9 1.400
## 16 1.16  9.9 252 56.0  9.2 15991  0.0 0.620
## 17 0.76  6.4 136 61.9  9.0  5714  8.3 1.920
## 18 1.05 12.6 150 56.7  2.7 10140  0.0 1.108
## 19 1.16 11.7 104 54.0 -2.1 13507  0.0 0.636
## 20 1.20 11.8 148 59.9  3.5  7287 41.1 0.702
## 21 1.04  8.6 204 61.0  3.5  6650  0.0 2.116
## 22 1.07  9.3 174 54.3  5.9 10093 26.6 1.306

Figure 1.1 and Figure 1.2

x1 <- c(3,4,2,6,8,2,5)
x2 <- c(5,5.5,4,7,10,5,7.5)
 
X1<- rbind(x1,x2)

x1 <- c(5,4,6,2,2,8,3)
x2 <- c(5,5.5,4,7,10,5,7.5)

X2<- rbind(x1,x2)

p <- ggplot(as.data.frame(t(X1)), aes(x=x1, y=x2, color=1, size=1)) +
          geom_point() +
            theme(legend.position="none")
p1 <- ggMarginal(p, type="histogram")


p2 <- ggplot(as.data.frame(t(X2)), aes(x=x1, y=x2, color=1, size=1)) +
  geom_point() +
  theme(legend.position="none")
p3 <- ggMarginal(p2, type="histogram")

grid.arrange(p1, p3, nrow = 1)

Example 1.4

par(mfrow=c(1,1))
plot(playroll[,1]/1000000, playroll[,2], xlab="Player payroll in millions of dollars", ylab="Won-lost percentage", ylim=c(0.35,0.7), col="blue", type="b")

Example 1.5

names(paper_quality)=c("Density", "Strength(MD)", "Strength(CD)" )
ggpairs(paper_quality, upper=list(continuous = "points", combo = "facethist", discrete = "facetbar", na = "na"))

Example 1.6, Example 1.7, and EXample 1.9

names(Lizard.size)<-list("Lizard", "Mass", "SVL", "HLS", "Sex")
plot3d(Lizard.size[,2:4], size=5)
mycolors <- c('royalblue1', 'darkcyan')
Lizard.size$color<-mycolors[as.numeric(Lizard.size$Sex)+1]
plot3d(Lizard.size[,2:4], col=Lizard.size$color,size=5)
plot3d(lumber[,1:3], size=5)

plot3d(lumber[,2:4], size=5)

plot3d(lumber[,c(1:2,4)], size=5)
plot3d(lumber[,c(1,3:4)], size=5)

Example 1.10

plot(x=c(2:5), xlab="Year", ylab="Weight", y=Bear[1,3:6], ylim=c(50,270), type="b", lty=1, pch=1,col=1)

for (i in 2:7){

  lines(x=c(2:5), y=Bear[i,2:5], ylim=c(50,270), type="b", lty=i,pch=i,col=i)
  
}

legend(2,250, c(1:7), cex=0.8, col=c(1:7), pch=1:7)


title(main="Combined growth curves for weight for seven female grizzly bears")

Example 1.11

stars(Utility)

Example 1.12

faces(Utility)

faces2(Utility)