pre
library(data.table)
library(pre)
setwd("C:/Users/subas/Syncplicity/MyProjects_IMP/MY_Papers_V2/TRB 2021/EScotter_BayesianRule/")
it01 <- fread("IT_aadtMaster.csv", header=T, na.strings=c("","NA"))
mn= subset(it01, State=="GA")
dim(mn)
## [1] 5334 86
## [1] 11498 86
## [1] 11498 86
mn01 <- mn[, c("Default_AADT", "HU", "Pop" , "WAC", "RAC", "Empl" )]
mn02= na.omit(mn01)
dim(mn02)
## [1] 5316 6
mn02= mn02[sample(nrow(mn02), 250), ]
set.seed(42)
mod1 <- pre(Default_AADT ~ ., data = mn02)
mod1
##
## Final ensemble with cv error within 1se of minimum:
## lambda = 38.13646
## number of terms = 10
## mean cv error (se) = 472597.2 (30920.29)
##
## cv error type : Mean-Squared Error
##
## rule coefficient description
## (Intercept) 1636.4670862 1
## rule34 -280.8879711 Empl <= 1398 & WAC <= 674
## rule102 -214.7705187 Empl <= 984 & HU > 584
## rule14 -187.2466233 RAC <= 686 & HU > 422
## rule91 -162.6968820 Empl <= 1043 & Pop > 724
## rule73 -122.3546197 WAC <= 301
## rule51 -63.9127533 Empl <= 534
## rule101 47.7922608 Empl <= 984 & HU <= 584 & Empl > 501
## rule68 -42.4592036 RAC <= 751 & HU > 418
## rule70 -15.1396132 RAC <= 778 & WAC <= 465
## rule88 -0.6022448 WAC <= 439
coefs <- coef(mod1)
coefs[1:6, ]
## rule coefficient description
## 107 (Intercept) 1636.4671 1
## 34 rule34 -280.8880 Empl <= 1398 & WAC <= 674
## 100 rule102 -214.7705 Empl <= 984 & HU > 584
## 14 rule14 -187.2466 RAC <= 686 & HU > 422
## 89 rule91 -162.6969 Empl <= 1043 & Pop > 724
## 71 rule73 -122.3546 WAC <= 301
imps <- importance(mod1, round = 4)

pairplot(mod1, varnames = c("WAC", "Empl"))


par(mfrow = c(2, 2))
expl <- explain(mod1, newdata = mn02[1:4, ], cex = .8)

plot(mod1, nterms = 9, cex = .5)

patchwork
## Warning: package 'ggplot2' was built under R version 3.6.2
## Warning: package 'patchwork' was built under R version 3.6.3
## Warning: package 'ggExtra' was built under R version 3.6.2
library(datasets)
quakes$magFactor <- factor(floor(quakes$mag))
quakes$region <- factor(quakes$long >= 175, labels = c("Ouest", "Est"))
str(quakes)
## 'data.frame': 1000 obs. of 7 variables:
## $ lat : num -20.4 -20.6 -26 -18 -20.4 ...
## $ long : num 182 181 184 182 182 ...
## $ depth : int 562 650 42 626 649 195 82 194 211 622 ...
## $ mag : num 4.8 4.2 5.4 4.1 4 4 4.8 4.4 4.7 4.3 ...
## $ stations : int 41 15 43 19 11 12 43 15 35 19 ...
## $ magFactor: Factor w/ 3 levels "4","5","6": 1 1 2 1 1 1 1 1 1 1 ...
## $ region : Factor w/ 2 levels "Ouest","Est": 2 2 2 2 2 2 1 2 2 2 ...
## lat long depth mag stations magFactor region
## 1 -20.42 181.62 562 4.8 41 4 Est
## 2 -20.62 181.03 650 4.2 15 4 Est
## 3 -26.00 184.10 42 5.4 43 5 Est
## 4 -17.97 181.66 626 4.1 19 4 Est
## 5 -20.42 181.96 649 4.0 11 4 Est
## 6 -19.68 184.31 195 4.0 12 4 Est
Grap1 <- ggplot(quakes, aes(mag, fill = region)) + theme_bw()+
geom_histogram(mapping = aes(y = stat(density)), binwidth = 0.1, color = "white")
Grap2 <- ggplot(data = quakes) +
geom_density(mapping = aes(x = mag, fill = region), alpha = 0.5) +theme_bw()+
labs(x = "x-axis") +
labs(y = "y-axis") +
labs(fill = "r攼㸹gion")
Grap3 <-ggplot(data = quakes) +
geom_violin(mapping = aes(x = region, y = mag)) +theme_bw()+
labs(x = "region") +
labs(y = "magnitude")
Grap4 <- ggplot(quakes, aes(x = magFactor, y = long, color = region, shape = region)) +theme_bw()+
geom_jitter(position = position_jitter(0.2))
p <- ggplot(quakes, aes(x = mag, y = stations, color=stations, size = stations)) +theme_bw()+ geom_point() +
theme(legend.position="none")
Grap5 <-`ggMarginal`(p, type = 'boxplot')
Grap1 + Grap2 + Grap3 + plot_layout(nrow = 1)

Grap1 + Grap2 +
{ Grap2 + {Grap4 + Grap3 + plot_layout(ncol = 1) }
} + Grap5 + plot_layout(nrow = 2)

Grap1 + Grap2 + Grap3 - Grap5 + plot_layout(ncol = 1) +plot_annotation(title = "title",
subtitle = "subtitle",
caption = "subasish_das", tag_levels = 'A')

## Warning: package 'ggpmisc' was built under R version 3.6.3
## Warning: package 'cowplot' was built under R version 3.6.2
##
## ********************************************************
## Note: As of version 1.0.0, cowplot does not change the
## default ggplot2 theme anymore. To recover the previous
## behavior, execute:
## theme_set(theme_cowplot())
## ********************************************************
##
## Attaching package: 'cowplot'
## The following object is masked from 'package:patchwork':
##
## align_plots
library(ggplot2)
library(ggpubr)
## Loading required package: magrittr
## Warning: package 'magrittr' was built under R version 3.6.2
##
## Attaching package: 'ggpubr'
## The following object is masked from 'package:cowplot':
##
## get_legend
theme_set(
theme_minimal() +
theme(legend.position = "top")
)
data("mtcars")
df <- mtcars
# Convert cyl as a grouping variable
df$cyl <- as.factor(df$cyl)
# Inspect the data
head(df[, c("wt", "mpg", "cyl", "qsec")], 4)
## wt mpg cyl qsec
## Mazda RX4 2.620 21.0 6 16.46
## Mazda RX4 Wag 2.875 21.0 6 17.02
## Datsun 710 2.320 22.8 4 18.61
## Hornet 4 Drive 3.215 21.4 6 19.44
b <- ggplot(df, aes(x = wt, y = mpg))
# Scatter plot with regression line
b + geom_point()+
geom_smooth(method = "lm")

# Add a loess smoothed fit curve
b + geom_point()+
geom_smooth(method = "loess")

# Change color and shape by groups (cyl)
grap1= b + geom_point(aes(color = cyl, shape = cyl))+
geom_smooth(aes(color = cyl, fill = cyl), method = "lm") +
geom_rug(aes(color =cyl)) +
scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))+
scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
# Remove confidence region (se = FALSE)
# Extend the regression lines: fullrange = TRUE
grap2= b + geom_point(aes(color = cyl, shape = cyl)) +
geom_rug(aes(color =cyl)) +
geom_smooth(aes(color = cyl), method = lm,
se = FALSE, fullrange = TRUE)+
scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))+
ggpubr::stat_cor(aes(color = cyl), label.x = 3)
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 3.6.2
# Add text to the plot
.labs <- rownames(df)
grap3= b + geom_point(aes(color = cyl)) +
geom_text_repel(aes(label = .labs, color = cyl), size = 3)+
scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
# Draw a rectangle underneath the text, making it easier to read.
grap4= b + geom_point(aes(color = cyl)) +
geom_label_repel(aes(label = .labs, color = cyl), size = 3)+
scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
# Use box plot as marginal plots
grap5= ggscatterhist(
iris, x = "Sepal.Length", y = "Sepal.Width",
color = "Species", size = 3, alpha = 0.6,
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
margin.plot = "boxplot",
ggtheme = theme_bw()
)
# Hexagonal binning
grap6= ggplot(diamonds, aes(carat, price)) +
geom_hex(bins = 20, color = "white")+
scale_fill_gradient(low = "#00AFBB", high = "#FC4E07")+
theme_minimal()
# Add 2d density estimation
sp <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(color = "lightgray")
grap7= sp + geom_density_2d()
# Use different geometry and change the gradient color
grap8=sp + stat_density_2d(aes(fill = ..level..), geom = "polygon") +
scale_fill_gradientn(colors = c("#FFEDA0", "#FEB24C", "#F03B20"))
grap1 + grap2 + grap3 + plot_layout(nrow = 1)

grap1 + grap2 +
{ grap5+ {grap6 + grap8 + plot_layout(ncol = 1) }
} + grap3+ plot_layout(nrow = 2)

grap1 + grap2 + grap3 - grap5 -grap8 + plot_layout(ncol = 1) +plot_annotation(title = "title",
subtitle = "subtitle",
caption = "subasish_das", tag_levels = 'A')
