options(repos = c(CRAN = "https://cran.r-project.org/"))
install.packages("GGally")
## 'C:/Users/billd/AppData/Local/R/win-library/4.3'μ μμΉμ ν¨ν€μ§(λ€)μ μ€μΉν©λλ€.
## (μλνλ©΄ 'lib'κ° μ§μ λμ§ μμκΈ° λλ¬Έμ
λλ€)
## ν¨ν€μ§ 'GGally'λ₯Ό μ±κ³΅μ μΌλ‘ μμΆν΄μ νμκ³ MD5 sums μ΄ νμΈλμμ΅λλ€
##
## λ€μ΄λ‘λλ λ°μ΄λ리 ν¨ν€μ§λ€μ λ€μμ μμΉμ μμ΅λλ€
## C:\Users\billd\AppData\Local\Temp\Rtmpwv0Bu8\downloaded_packages
install.packages("caret")
## 'C:/Users/billd/AppData/Local/R/win-library/4.3'μ μμΉμ ν¨ν€μ§(λ€)μ μ€μΉν©λλ€.
## (μλνλ©΄ 'lib'κ° μ§μ λμ§ μμκΈ° λλ¬Έμ
λλ€)
## ν¨ν€μ§ 'caret'λ₯Ό μ±κ³΅μ μΌλ‘ μμΆν΄μ νμκ³ MD5 sums μ΄ νμΈλμμ΅λλ€
## Warning: ν¨ν€μ§ 'caret'μ μ΄μ μ€μΉλ₯Ό μμ ν μ μμ΅λλ€
## Warning in file.copy(savedcopy, lib, recursive = TRUE):
## C:\Users\billd\AppData\Local\R\win-library\4.3\00LOCK\caret\libs\x64\caret.dllλ₯Ό
## C:\Users\billd\AppData\Local\R\win-library\4.3\caret\libs\x64\caret.dllλ‘
## 볡μ¬νλλ° λ¬Έμ κ° λ°μνμ΅λλ€: Permission denied
## Warning: 'caret'λ₯Ό 볡ꡬνμμ΅λλ€
##
## λ€μ΄λ‘λλ λ°μ΄λ리 ν¨ν€μ§λ€μ λ€μμ μμΉμ μμ΅λλ€
## C:\Users\billd\AppData\Local\Temp\Rtmpwv0Bu8\downloaded_packages
install.packages("partykit")
## 'C:/Users/billd/AppData/Local/R/win-library/4.3'μ μμΉμ ν¨ν€μ§(λ€)μ μ€μΉν©λλ€.
## (μλνλ©΄ 'lib'κ° μ§μ λμ§ μμκΈ° λλ¬Έμ
λλ€)
## ν¨ν€μ§ 'partykit'λ₯Ό μ±κ³΅μ μΌλ‘ μμΆν΄μ νμκ³ MD5 sums μ΄ νμΈλμμ΅λλ€
## Warning: ν¨ν€μ§ 'partykit'μ μ΄μ μ€μΉλ₯Ό μμ ν μ μμ΅λλ€
## Warning in file.copy(savedcopy, lib, recursive = TRUE):
## C:\Users\billd\AppData\Local\R\win-library\4.3\00LOCK\partykit\libs\x64\partykit.dllλ₯Ό
## C:\Users\billd\AppData\Local\R\win-library\4.3\partykit\libs\x64\partykit.dllλ‘
## 볡μ¬νλλ° λ¬Έμ κ° λ°μνμ΅λλ€: Permission denied
## Warning: 'partykit'λ₯Ό 볡ꡬνμμ΅λλ€
##
## λ€μ΄λ‘λλ λ°μ΄λ리 ν¨ν€μ§λ€μ λ€μμ μμΉμ μμ΅λλ€
## C:\Users\billd\AppData\Local\Temp\Rtmpwv0Bu8\downloaded_packages
install.packages("rpart")
## 'C:/Users/billd/AppData/Local/R/win-library/4.3'μ μμΉμ ν¨ν€μ§(λ€)μ μ€μΉν©λλ€.
## (μλνλ©΄ 'lib'κ° μ§μ λμ§ μμκΈ° λλ¬Έμ
λλ€)
## ν¨ν€μ§ 'rpart'λ₯Ό μ±κ³΅μ μΌλ‘ μμΆν΄μ νμκ³ MD5 sums μ΄ νμΈλμμ΅λλ€
##
## λ€μ΄λ‘λλ λ°μ΄λ리 ν¨ν€μ§λ€μ λ€μμ μμΉμ μμ΅λλ€
## C:\Users\billd\AppData\Local\Temp\Rtmpwv0Bu8\downloaded_packages
install.packages("ggplot2")
## 'C:/Users/billd/AppData/Local/R/win-library/4.3'μ μμΉμ ν¨ν€μ§(λ€)μ μ€μΉν©λλ€.
## (μλνλ©΄ 'lib'κ° μ§μ λμ§ μμκΈ° λλ¬Έμ
λλ€)
## ν¨ν€μ§ 'ggplot2'λ₯Ό μ±κ³΅μ μΌλ‘ μμΆν΄μ νμκ³ MD5 sums μ΄ νμΈλμμ΅λλ€
##
## λ€μ΄λ‘λλ λ°μ΄λ리 ν¨ν€μ§λ€μ λ€μμ μμΉμ μμ΅λλ€
## C:\Users\billd\AppData\Local\Temp\Rtmpwv0Bu8\downloaded_packages
install.packages("patchwork")
## 'C:/Users/billd/AppData/Local/R/win-library/4.3'μ μμΉμ ν¨ν€μ§(λ€)μ μ€μΉν©λλ€.
## (μλνλ©΄ 'lib'κ° μ§μ λμ§ μμκΈ° λλ¬Έμ
λλ€)
## ν¨ν€μ§ 'patchwork'λ₯Ό μ±κ³΅μ μΌλ‘ μμΆν΄μ νμκ³ MD5 sums μ΄ νμΈλμμ΅λλ€
##
## λ€μ΄λ‘λλ λ°μ΄λ리 ν¨ν€μ§λ€μ λ€μμ μμΉμ μμ΅λλ€
## C:\Users\billd\AppData\Local\Temp\Rtmpwv0Bu8\downloaded_packages
install.packages("tidyverse")
## 'C:/Users/billd/AppData/Local/R/win-library/4.3'μ μμΉμ ν¨ν€μ§(λ€)μ μ€μΉν©λλ€.
## (μλνλ©΄ 'lib'κ° μ§μ λμ§ μμκΈ° λλ¬Έμ
λλ€)
## ν¨ν€μ§ 'tidyverse'λ₯Ό μ±κ³΅μ μΌλ‘ μμΆν΄μ νμκ³ MD5 sums μ΄ νμΈλμμ΅λλ€
##
## λ€μ΄λ‘λλ λ°μ΄λ리 ν¨ν€μ§λ€μ λ€μμ μμΉμ μμ΅λλ€
## C:\Users\billd\AppData\Local\Temp\Rtmpwv0Bu8\downloaded_packages
install.packages("nnet")
## 'C:/Users/billd/AppData/Local/R/win-library/4.3'μ μμΉμ ν¨ν€μ§(λ€)μ μ€μΉν©λλ€.
## (μλνλ©΄ 'lib'κ° μ§μ λμ§ μμκΈ° λλ¬Έμ
λλ€)
## ν¨ν€μ§ 'nnet'λ₯Ό μ±κ³΅μ μΌλ‘ μμΆν΄μ νμκ³ MD5 sums μ΄ νμΈλμμ΅λλ€
##
## λ€μ΄λ‘λλ λ°μ΄λ리 ν¨ν€μ§λ€μ λ€μμ μμΉμ μμ΅λλ€
## C:\Users\billd\AppData\Local\Temp\Rtmpwv0Bu8\downloaded_packages
library(GGally)
## Warning: ν¨ν€μ§ 'GGally'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## νμν ν¨ν€μ§λ₯Ό λ‘λ©μ€μ
λλ€: ggplot2
## Warning: ν¨ν€μ§ 'ggplot2'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
library(caret)
## Warning: ν¨ν€μ§ 'caret'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## νμν ν¨ν€μ§λ₯Ό λ‘λ©μ€μ
λλ€: lattice
library(partykit)
## Warning: ν¨ν€μ§ 'partykit'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## νμν ν¨ν€μ§λ₯Ό λ‘λ©μ€μ
λλ€: grid
## νμν ν¨ν€μ§λ₯Ό λ‘λ©μ€μ
λλ€: libcoin
## Warning: ν¨ν€μ§ 'libcoin'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## νμν ν¨ν€μ§λ₯Ό λ‘λ©μ€μ
λλ€: mvtnorm
## Warning: ν¨ν€μ§ 'mvtnorm'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
library(rpart)
## Warning: ν¨ν€μ§ 'rpart'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
library(ggplot2)
library(patchwork)
## Warning: ν¨ν€μ§ 'patchwork'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
library(tidyverse)
## Warning: ν¨ν€μ§ 'tidyverse'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## Warning: ν¨ν€μ§ 'tibble'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## Warning: ν¨ν€μ§ 'tidyr'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## Warning: ν¨ν€μ§ 'readr'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## Warning: ν¨ν€μ§ 'purrr'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## Warning: ν¨ν€μ§ 'dplyr'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## Warning: ν¨ν€μ§ 'stringr'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## Warning: ν¨ν€μ§ 'forcats'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## Warning: ν¨ν€μ§ 'lubridate'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
## ββ Attaching core tidyverse packages ββββββββββββββββββββββββ tidyverse 2.0.0 ββ
## β dplyr 1.1.4 β readr 2.1.5
## β forcats 1.0.0 β stringr 1.5.1
## β lubridate 1.9.3 β tibble 3.2.1
## β purrr 1.0.2 β tidyr 1.3.1
## ββ Conflicts ββββββββββββββββββββββββββββββββββββββββββ tidyverse_conflicts() ββ
## β dplyr::filter() masks stats::filter()
## β dplyr::lag() masks stats::lag()
## β purrr::lift() masks caret::lift()
## βΉ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(nnet)
## Warning: ν¨ν€μ§ 'nnet'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
install.packages("conflicted")
## 'C:/Users/billd/AppData/Local/R/win-library/4.3'μ μμΉμ ν¨ν€μ§(λ€)μ μ€μΉν©λλ€.
## (μλνλ©΄ 'lib'κ° μ§μ λμ§ μμκΈ° λλ¬Έμ
λλ€)
## ν¨ν€μ§ 'conflicted'λ₯Ό μ±κ³΅μ μΌλ‘ μμΆν΄μ νμκ³ MD5 sums μ΄ νμΈλμμ΅λλ€
##
## λ€μ΄λ‘λλ λ°μ΄λ리 ν¨ν€μ§λ€μ λ€μμ μμΉμ μμ΅λλ€
## C:\Users\billd\AppData\Local\Temp\Rtmpwv0Bu8\downloaded_packages
library(conflicted)
## Warning: ν¨ν€μ§ 'conflicted'λ R λ²μ 4.3.3μμ μμ±λμμ΅λλ€
conflict_prefer("filter", "dplyr")
## [conflicted] Will prefer dplyr::filter over any other package.
conflict_prefer("lag", "dplyr")
## [conflicted] Will prefer dplyr::lag over any other package.
conflict_prefer("lift", "purrr")
## [conflicted] Will prefer purrr::lift over any other package.
set_olive <- function(inc=c(1,2)){
require(tidyverse, quietly = TRUE)
if(!("cepp" %in% .packages(all = TRUE))) install.packages("cepp")
require(cepp, quietly = TRUE)
data(olive)
olivex <- olive
names(olivex) <- c("rg","ar","pt","po","st","ol","lo","ln","ac","es")
olivex
rg <- factor(olivex$rg)
levels(rg) <- c("S","R","N")
olivex$rg <- factor(rg, levels=c("S","R","N"))
ar <- factor(olivex$ar)
arf <- factor(ar, levels=levels(ar)[c(6,1,7,5,2,4,8,3,9)])
x <- levels(arf) %>%
tolower() %>%
str_replace("east","e") %>%
str_replace("west","w") %>%
str_replace("south","s") %>%
str_replace("north","n") %>%
str_replace("coast","c") %>%
str_replace("inland","i")
y <- str_split(x,"-",simplify=TRUE)
y1 <- str_sub(y[,1],1,4)
y2 <- str_sub(y[,2],1,4)
short <- paste0(c("S","S","S","S","R","R","N","N","N"),
"_", y1,"_", y2) %>% str_replace_all("_$","")
levels(arf) <- short
olivex$ar <- arf
indx <- c(unique(c(inc, 3:10)))
olivex_subset <- olivex[, indx]
return(olivex_subset)
}
# olive1, olive2 μμ±
olive1 <- set_olive(1)
olive2 <- set_olive(2)
# olive1κ³Ό olive2μμ ν
μ΄λΈ μμ±νκΈ°
with(olive1, table(rg))
## rg
## S R N
## 323 98 151
with(olive2, table(ar))
## ar
## S_sici S_cala S_s_apul S_n_apul R_c_sard R_i_sard N_umbr N_e_ligu
## 36 56 206 25 33 65 51 50
## N_w_ligu
## 50
with(set_olive(), table(rg, ar))
## ar
## rg S_sici S_cala S_s_apul S_n_apul R_c_sard R_i_sard N_umbr N_e_ligu N_w_ligu
## S 36 56 206 25 0 0 0 0 0
## R 0 0 0 0 33 65 0 0 0
## N 0 0 0 0 0 0 51 50 50
type_fix <- function(res){
type=class(res)
if('multinom' %in% type) type='multinom' # nnet
if('lda' %in% type) type='lda' # MASS
if('qda' %in% type) type='qda' # MASS
if('nnet' %in% type) type='nnet' # nnet
if('rpart' %in% type) type='rpart' # rpart
if('party' %in% type) type='party' # partikit
if('svm' %in% type) type='svm' # e1071
if('train' %in% type) type='train' # caret
type
}
predict_cases<-function(res,new){
type = type_fix(res)
p_sp =rep( '0', nrow(new) )
if(type %in% c('lda','qda')) p_sp =
predict(res, newdata=new)$class
if(type %in% c('multinom','rpart','nnet')) p_sp =
predict(res, newdata=new, type="class")
if(type %in% c('party','svm')) p_sp =
as.vector(predict(res, newdata=new))
if(type %in% c('train')) p_sp =
predict(res, newdata=new, type="raw")
as.character(p_sp)
}
is_a_leaf <-function(fm){
out = TRUE
if( is.call(fm) )
if( length(fm)>2 )
if( (fm[[1]]=='+')|(fm[[1]]=='-') ) out=FALSE
out
}
leafs <- function(res){
v = NULL
fx = res
while(!is_a_leaf(fx)){
v = c(fx[[3]],v)
fx = fx[[2]]
}
c(fx,v)
}
Draw <- function(res, border=1, axis=1, np=100){
type = type_fix(res)
callx = res$call
df = callx[[3]]
fm = callx[[2]]
if( type %in% c('party') ){
df0 = res$data
df = as.name('df0')
fm = res$terms }
vy = fm[[2]]
terms = leafs(fm[[3]])
cterms = as.character(terms)
pos = which(sapply(terms, is.name))
q1 = as.name(cterms[pos][1])
q2 = as.name(cterms[pos][2])
if(axis==0){
q1 = as.name(cterms[pos][2])
q2 = as.name(cterms[pos][1]) }
x1 = eval(q1, eval(df))
x2 = eval(q2, eval(df))
x1q = seq( min(x1), max(x1), l=np)
x2q = seq( min(x2), max(x2), l=np)
new = expand.grid( x=x1q, y=x2q )
names(new) = as.character(c(q1,q2))
cp_0 = predict_cases(res,eval(df))
cp_1 = as.character(eval(vy, eval(df)))
p_1 = factor(cp_1)
dfz = eval(df)[cp_0!=cp_1,]
dfz$p = p_1[cp_0!=cp_1]
cp_sp = predict_cases(res,new)
p_sp = factor(cp_sp, levels=levels(p_1))
new_p = data.frame( new, p_sp )
p<- eval( bquote(
ggplot(.(df)) +
geom_point( aes(.(q1),.(q2), col=.(vy)))+
geom_point(data= new_p,
aes(.(q1),.(q2), col=p_sp),size=0.1,alpha=0.2) +
geom_point(data=dfz,
aes(.(q1),.(q2),col=p ),shape=1,size=4)+
geom_point( aes(.(q1),.(q2), col=.(vy)), size=1.4)
#+theme_light()
) )
if(border==1){
z_sp = factor(cp_sp)
K = length(levels(z_sp))
if(K>1){
zzz<-diag(K)[z_sp,]
for(k in 1:K){
new_z <- new_p
new_z$z <- zzz[,k]
p<-p + eval( bquote( geom_contour(data=new_z,
aes(.(q1),.(q2),z=z),
breaks=0.5, size=0.1, col="black")
))
}
}
}
p
}
#νλ²μ΄ 6μΌλ‘ λλλ―λ‘ 2. palmitoleic ( po )κ³Ό 4. oleic ( ol ) λ°μ΄ν° λΆμ
roc_LDA <- train(ar ~ ol + po, olive2, method="lda")
roc_NNET <- train(ar ~ ol + po, olive2, method="nnet",
maxit=300, trace=F)
roc_TREE <- train(ar ~ ol + po, olive2, method="rpart")
roc01a_TREE <- as.party(rpart(ar ~ ol + po, olive2, cp=0.01))
roc_SVM <- train(ar ~ ol + po, olive2, method="svmRadial")
( pm <- t(roc_TREE$bestTune))
## 1
## cp 0.1229508
x1<- table(olive2$ar, predict(roc_LDA))
x2<- table(olive2$ar, predict(roc_NNET))
x3<- table(olive2$ar, predict(roc_TREE))
x301a<- table(olive2$ar, predict(roc01a_TREE))
x4<- table(olive2$ar, predict(roc_SVM))
sum(diag(x1))/sum(x1)
## [1] 0.7185315
sum(diag(x2))/sum(x2)
## [1] 0.7062937
sum(diag(x3))/sum(x3)
## [1] 0.5594406
sum(diag(x301a))/sum(x301a)
## [1] 0.777972
sum(diag(x4))/sum(x4)
## [1] 0.7727273
p1<- Draw(roc_LDA)+labs(title="LDA")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## βΉ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
p2<- Draw(roc_NNET)+labs(title="NNET")
p3<- Draw(roc01a_TREE)+labs(title="TREE")
p4<- Draw(roc_SVM)+labs(title="SVM")
(p1+p2)/(p3+p4)+plot_layout(guides="collect")
#νλ²μ΄ 6μΌλ‘ λλλ―λ‘ 1. palmitic ( pt )κ³Ό 5. linolenic ( ln ) λ°μ΄ν° λΆμ
roc_LDA <- train(ar ~ pt + ln, olive2, method="lda")
roc_NNET <- train(ar ~ pt + ln, olive2, method="nnet",
maxit=300, trace=F)
roc_TREE <- train(ar ~ pt + ln, olive2, method="rpart")
roc01a_TREE <- as.party(rpart(ar ~ pt + ln, olive2, cp=0.01))
roc_SVM <- train(ar ~ pt + ln, olive2, method="svmRadial")
( pm <- t(roc_TREE$bestTune) )
## 1
## cp 0.04644809
x1<- table(olive2$ar, predict(roc_LDA))
x2<- table(olive2$ar, predict(roc_NNET))
x3<- table(olive2$ar, predict(roc_TREE))
x301a<- table(olive2$ar, predict(roc01a_TREE))
x4<- table(olive2$ar, predict(roc_SVM))
sum(diag(x1))/sum(x1)
## [1] 0.6416084
sum(diag(x2))/sum(x2)
## [1] 0.6555944
sum(diag(x3))/sum(x3)
## [1] 0.5541958
sum(diag(x301a))/sum(x301a)
## [1] 0.736014
sum(diag(x4))/sum(x4)
## [1] 0.708042
p1<- Draw(roc_LDA)+labs(title="LDA")
p2<- Draw(roc_NNET)+labs(title="NNET")
p3<- Draw(roc01a_TREE)+labs(title="TREE")
p4<- Draw(roc_SVM)+labs(title="SVM")
(p1+p2)/(p3+p4)+plot_layout(guides="collect")