getwd()
## [1] "C:/data"
setwd('c:/data')
library(dplyr)
##
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(caret)
## 필요한 패키지를 로딩중입니다: ggplot2
## 필요한 패키지를 로딩중입니다: lattice
data(iris)
glimpse(iris)
## Rows: 150
## Columns: 5
## $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.…
## $ Sepal.Width <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.…
## $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.…
## $ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.…
## $ Species <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s…
iris %>% count(Species)
## Species n
## 1 setosa 50
## 2 versicolor 50
## 3 virginica 50
library(randomForest)
## Warning: 패키지 'randomForest'는 R 버전 4.3.2에서 작성되었습니다
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
##
## 다음의 패키지를 부착합니다: 'randomForest'
## The following object is masked from 'package:ggplot2':
##
## margin
## The following object is masked from 'package:dplyr':
##
## combine
#모델 생성
model<-randomForest(Species~.,data=iris)
model
##
## Call:
## randomForest(formula = Species ~ ., data = iris)
## Type of random forest: classification
## Number of trees: 500
## No. of variables tried at each split: 2
##
## OOB estimate of error rate: 4.67%
## Confusion matrix:
## setosa versicolor virginica class.error
## setosa 50 0 0 0.00
## versicolor 0 47 3 0.06
## virginica 0 4 46 0.08
#파라미터 조정
model2<-randomForest(Species~.,data=iris,
ntree=300,mtry=4,na.action=na.omit)
#ntree 의사결정나무수
#mtry 변수의 수
#na.action=na.omit, 결측값 확인
model2
##
## Call:
## randomForest(formula = Species ~ ., data = iris, ntree = 300, mtry = 4, na.action = na.omit)
## Type of random forest: classification
## Number of trees: 300
## No. of variables tried at each split: 4
##
## OOB estimate of error rate: 4%
## Confusion matrix:
## setosa versicolor virginica class.error
## setosa 50 0 0 0.00
## versicolor 0 47 3 0.06
## virginica 0 3 47 0.06
#중요변수로 랜덤포레스트 생성
model3<-randomForest(Species~.,data=iris,
importance=TRUE,na.action=na.omit)
importance(model3)
## setosa versicolor virginica MeanDecreaseAccuracy
## Sepal.Length 5.826774 7.8056766 7.728592 10.861639
## Sepal.Width 4.434457 -0.2905671 5.171652 4.819673
## Petal.Length 24.900829 35.1946272 31.944578 37.578464
## Petal.Width 19.811710 30.7715356 30.068708 31.858962
## MeanDecreaseGini
## Sepal.Length 8.967859
## Sepal.Width 2.192184
## Petal.Length 45.335803
## Petal.Width 42.789207
varImpPlot(model3)

#최적의 파라미터 찾기
ntree<-c(400,500,600)
mtry<-c(2:4)
param<-data.frame(n=ntree,m=mtry)
for(i in param$n){
cat('ntree=',i,'\n')
for(j in param$m) {
cat('mtry=',j,'\n')
model_iris<-randomForest(Species~.,data=iris,
ntree=i,mtry=j,na.action=na.omit)
print(model_iris)
}
}
## ntree= 400
## mtry= 2
##
## Call:
## randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j, na.action = na.omit)
## Type of random forest: classification
## Number of trees: 400
## No. of variables tried at each split: 2
##
## OOB estimate of error rate: 4.67%
## Confusion matrix:
## setosa versicolor virginica class.error
## setosa 50 0 0 0.00
## versicolor 0 46 4 0.08
## virginica 0 3 47 0.06
## mtry= 3
##
## Call:
## randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j, na.action = na.omit)
## Type of random forest: classification
## Number of trees: 400
## No. of variables tried at each split: 3
##
## OOB estimate of error rate: 4%
## Confusion matrix:
## setosa versicolor virginica class.error
## setosa 50 0 0 0.00
## versicolor 0 47 3 0.06
## virginica 0 3 47 0.06
## mtry= 4
##
## Call:
## randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j, na.action = na.omit)
## Type of random forest: classification
## Number of trees: 400
## No. of variables tried at each split: 4
##
## OOB estimate of error rate: 4.67%
## Confusion matrix:
## setosa versicolor virginica class.error
## setosa 50 0 0 0.00
## versicolor 0 47 3 0.06
## virginica 0 4 46 0.08
## ntree= 500
## mtry= 2
##
## Call:
## randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j, na.action = na.omit)
## Type of random forest: classification
## Number of trees: 500
## No. of variables tried at each split: 2
##
## OOB estimate of error rate: 4%
## Confusion matrix:
## setosa versicolor virginica class.error
## setosa 50 0 0 0.00
## versicolor 0 47 3 0.06
## virginica 0 3 47 0.06
## mtry= 3
##
## Call:
## randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j, na.action = na.omit)
## Type of random forest: classification
## Number of trees: 500
## No. of variables tried at each split: 3
##
## OOB estimate of error rate: 4%
## Confusion matrix:
## setosa versicolor virginica class.error
## setosa 50 0 0 0.00
## versicolor 0 47 3 0.06
## virginica 0 3 47 0.06
## mtry= 4
##
## Call:
## randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j, na.action = na.omit)
## Type of random forest: classification
## Number of trees: 500
## No. of variables tried at each split: 4
##
## OOB estimate of error rate: 4%
## Confusion matrix:
## setosa versicolor virginica class.error
## setosa 50 0 0 0.00
## versicolor 0 47 3 0.06
## virginica 0 3 47 0.06
## ntree= 600
## mtry= 2
##
## Call:
## randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j, na.action = na.omit)
## Type of random forest: classification
## Number of trees: 600
## No. of variables tried at each split: 2
##
## OOB estimate of error rate: 4%
## Confusion matrix:
## setosa versicolor virginica class.error
## setosa 50 0 0 0.00
## versicolor 0 47 3 0.06
## virginica 0 3 47 0.06
## mtry= 3
##
## Call:
## randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j, na.action = na.omit)
## Type of random forest: classification
## Number of trees: 600
## No. of variables tried at each split: 3
##
## OOB estimate of error rate: 4%
## Confusion matrix:
## setosa versicolor virginica class.error
## setosa 50 0 0 0.00
## versicolor 0 47 3 0.06
## virginica 0 3 47 0.06
## mtry= 4
##
## Call:
## randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j, na.action = na.omit)
## Type of random forest: classification
## Number of trees: 600
## No. of variables tried at each split: 4
##
## OOB estimate of error rate: 4.67%
## Confusion matrix:
## setosa versicolor virginica class.error
## setosa 50 0 0 0.00
## versicolor 0 47 3 0.06
## virginica 0 4 46 0.08
library(gapminder)
## Warning: 패키지 'gapminder'는 R 버전 4.3.2에서 작성되었습니다
data("gapminder")
library(dplyr)
glimpse(gapminder)
## Rows: 1,704
## Columns: 6
## $ country <fct> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.8…
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, …
gapminder %>% count(year)
## # A tibble: 12 × 2
## year n
## <int> <int>
## 1 1952 142
## 2 1957 142
## 3 1962 142
## 4 1967 142
## 5 1972 142
## 6 1977 142
## 7 1982 142
## 8 1987 142
## 9 1992 142
## 10 1997 142
## 11 2002 142
## 12 2007 142
gapminder %>% filter(year=='2007') %>%
mutate(totalGdp=pop*gdpPercap)
## # A tibble: 142 × 7
## country continent year lifeExp pop gdpPercap totalGdp
## <fct> <fct> <int> <dbl> <int> <dbl> <dbl>
## 1 Afghanistan Asia 2007 43.8 31889923 975. 31079291949.
## 2 Albania Europe 2007 76.4 3600523 5937. 21376411360.
## 3 Algeria Africa 2007 72.3 33333216 6223. 207444851958.
## 4 Angola Africa 2007 42.7 12420476 4797. 59583895818.
## 5 Argentina Americas 2007 75.3 40301927 12779. 515033625357.
## 6 Australia Oceania 2007 81.2 20434176 34435. 703658358894.
## 7 Austria Europe 2007 79.8 8199783 36126. 296229400691.
## 8 Bahrain Asia 2007 75.6 708573 29796. 21112675360.
## 9 Bangladesh Asia 2007 64.1 150448339 1391. 209311822134.
## 10 Belgium Europe 2007 79.4 10392226 33693. 350141166520.
## # ℹ 132 more rows
gapminder<-gapminder%>% filter(year=='2007') %>%
mutate(totalGdp=pop*gdpPercap)
gapminder %>% filter(year=='2007') %>%
mutate(totalGdp=pop*gdpPercap) %>%
summarize(avg=mean(totalGdp))
## # A tibble: 1 × 1
## avg
## <dbl>
## 1 409220666999.
gapminder %>% filter(totalGdp>=409220666999) %>% NROW()
## [1] 25
gapminder %>% filter(totalGdp>=409220666999) %>% arrange(desc(totalGdp))
## # A tibble: 25 × 7
## country continent year lifeExp pop gdpPercap totalGdp
## <fct> <fct> <int> <dbl> <int> <dbl> <dbl>
## 1 United States Americas 2007 78.2 301139947 42952. 1.29e13
## 2 China Asia 2007 73.0 1318683096 4959. 6.54e12
## 3 Japan Asia 2007 82.6 127467972 31656. 4.04e12
## 4 India Asia 2007 64.7 1110396331 2452. 2.72e12
## 5 Germany Europe 2007 79.4 82400996 32170. 2.65e12
## 6 United Kingdom Europe 2007 79.4 60776238 33203. 2.02e12
## 7 France Europe 2007 80.7 61083916 30470. 1.86e12
## 8 Brazil Americas 2007 72.4 190010647 9066. 1.72e12
## 9 Italy Europe 2007 80.5 58147733 28570. 1.66e12
## 10 Mexico Americas 2007 76.2 108700891 11978. 1.30e12
## # ℹ 15 more rows
data("gapminder")
gapminder %>% filter(year=='1997') %>%
mutate(totalGdp=pop*gdpPercap)
## # A tibble: 142 × 7
## country continent year lifeExp pop gdpPercap totalGdp
## <fct> <fct> <int> <dbl> <int> <dbl> <dbl>
## 1 Afghanistan Asia 1997 41.8 22227415 635. 14121995875.
## 2 Albania Europe 1997 73.0 3428038 3193. 10945912519.
## 3 Algeria Africa 1997 69.2 29072015 4797. 139467033682.
## 4 Angola Africa 1997 41.0 9875024 2277. 22486820881.
## 5 Argentina Americas 1997 73.3 36203463 10967. 397053586287.
## 6 Australia Oceania 1997 78.8 18565243 26998. 501223252921.
## 7 Austria Europe 1997 77.5 8069876 29096. 234800471832.
## 8 Bahrain Asia 1997 73.9 598561 20292. 12146009862.
## 9 Bangladesh Asia 1997 59.4 123315288 973. 119957417048.
## 10 Belgium Europe 1997 77.5 10199787 27561. 281118335091.
## # ℹ 132 more rows
gapminder<-gapminder%>% filter(year=='1997') %>%
mutate(totalGdp=pop*gdpPercap)
gapminder %>% filter(year=='1997') %>%
mutate(totalGdp=pop*gdpPercap) %>%
summarize(avg=mean(totalGdp))
## # A tibble: 1 × 1
## avg
## <dbl>
## 1 288775189215.
gapminder %>% filter(totalGdp>=288775189215) %>% arrange(desc(totalGdp))
## # A tibble: 24 × 7
## country continent year lifeExp pop gdpPercap totalGdp
## <fct> <fct> <int> <dbl> <int> <dbl> <dbl>
## 1 United States Americas 1997 76.8 272911760 35767. 9.76e12
## 2 Japan Asia 1997 80.7 125956499 28817. 3.63e12
## 3 China Asia 1997 70.4 1230075000 2289. 2.82e12
## 4 Germany Europe 1997 77.3 82011073 27789. 2.28e12
## 5 United Kingdom Europe 1997 77.2 58808266 26075. 1.53e12
## 6 France Europe 1997 78.6 58623428 25890. 1.52e12
## 7 Italy Europe 1997 78.8 57479469 24675. 1.42e12
## 8 India Asia 1997 61.8 959000000 1459. 1.40e12
## 9 Brazil Americas 1997 69.4 168546719 7958. 1.34e12
## 10 Mexico Americas 1997 73.7 95895146 9767. 9.37e11
## # ℹ 14 more rows