Covid19 merubah segalanya, termasuk cara belajar kita, karena kursus https://bambangpe.shinyapps.io/kursus-r2/ belum bisa mengadakan tatap muka, maka saya coba untuk menjembatani dengan cara yang lain.Bagi yang ingin belajar Bahasa R/R coding, supaya lebih mandiri, artinya sudah bisa menginstal Rstudio: https://rstudio.com/products/rstudio/download/ di laptop sendiri(tutorial instal Rstudio ada di internet).Lewat media ini saya akan melewati Rcoding basic(yang ini juga banyak di internet), saya akan sedikit meloncat untuk memulai sedikit belajar analisa. Diambil contoh data Boston(Housing Values in Suburbs of Boston)dari library(MASS) dan akan dicari variable apa yang mempengaruhi harga perumahan tsb. Penjelasan tentang data Boston bisa diketik di console Rstudio dengan command: ?Boston, gampang kan mudah dan cepat. Ayo tetap semangat, ikuti posting saya jalan menuju data scientist ada di sini!
library(ggplot2)# membuat grafik
library(dplyr)# proses perhitungan data
library(MASS)# data
library(randomForest)# membuat modeldata <- Boston
dim(data)
#> [1] 506 14
head(data,5)
#> crim zn indus chas nox rm age dis rad tax ptratio black lstat
#> 1 0.00632 18 2.31 0 0.538 6.575 65.2 4.0900 1 296 15.3 396.90 4.98
#> 2 0.02731 0 7.07 0 0.469 6.421 78.9 4.9671 2 242 17.8 396.90 9.14
#> 3 0.02729 0 7.07 0 0.469 7.185 61.1 4.9671 2 242 17.8 392.83 4.03
#> 4 0.03237 0 2.18 0 0.458 6.998 45.8 6.0622 3 222 18.7 394.63 2.94
#> 5 0.06905 0 2.18 0 0.458 7.147 54.2 6.0622 3 222 18.7 396.90 5.33
#> medv
#> 1 24.0
#> 2 21.6
#> 3 34.7
#> 4 33.4
#> 5 36.2
dt1 <- data[ ,-4]
head(dt1,5)
#> crim zn indus nox rm age dis rad tax ptratio black lstat medv
#> 1 0.00632 18 2.31 0.538 6.575 65.2 4.0900 1 296 15.3 396.90 4.98 24.0
#> 2 0.02731 0 7.07 0.469 6.421 78.9 4.9671 2 242 17.8 396.90 9.14 21.6
#> 3 0.02729 0 7.07 0.469 7.185 61.1 4.9671 2 242 17.8 392.83 4.03 34.7
#> 4 0.03237 0 2.18 0.458 6.998 45.8 6.0622 3 222 18.7 394.63 2.94 33.4
#> 5 0.06905 0 2.18 0.458 7.147 54.2 6.0622 3 222 18.7 396.90 5.33 36.2Dalam ilmu statistik, jika data yang akan dianalisa jumlahnya besar, maka diambil samplenya saja
spl <- sample(nrow(dt1),nrow(dt1)*0.7)
train <- dt1[spl, ]
test <- dt1[-spl, ]Ada banyak model untuk analisa, dalam latihan ini saya menggunakan model Random Forest
model <- randomForest(medv~., data = train)Plot model untuk memebri gambaran model yang kita analisa
pmod <- plot(model)pmod+theme_linedraw()
#> NULLModel Random Forest adalah model dari banyak model tree yang dijadikan satu, artinya makin banyak data sample yang dibuat makin valid hasilnya, dari grafik bisa dilihat setelah tree ke 500 grafiknya jadi flat, itu artinya model ini relevan dan dapat dipercaya.
pm <- summary(model)
pm
#> Length Class Mode
#> call 3 -none- call
#> type 1 -none- character
#> predicted 354 -none- numeric
#> mse 500 -none- numeric
#> rsq 500 -none- numeric
#> oob.times 354 -none- numeric
#> importance 12 -none- numeric
#> importanceSD 0 -none- NULL
#> localImportance 0 -none- NULL
#> proximity 0 -none- NULL
#> ntree 1 -none- numeric
#> mtry 1 -none- numeric
#> forest 11 -none- list
#> coefs 0 -none- NULL
#> y 354 -none- numeric
#> test 0 -none- NULL
#> inbag 0 -none- NULL
#> terms 3 terms callpv <- varImpPlot(model)pv
#> IncNodePurity
#> crim 1927.7068
#> zn 174.0315
#> indus 1488.9340
#> nox 1387.5425
#> rm 7613.5301
#> age 641.7405
#> dis 1153.0748
#> rad 165.3988
#> tax 1034.4590
#> ptratio 1672.8161
#> black 480.2291
#> lstat 7824.1468var_importance <- data_frame(variable = setdiff(colnames(train), "medv"),
importance = as.vector(importance(model)))
var_importance <- arrange(var_importance, desc(importance))
#var_importance
var_importance$variable <- factor(var_importance$variable, levels=var_importance$variable)
pg <- ggplot(data = var_importance, aes(x = variable, y = importance))+
geom_bar(stat = "identity")+ggtitle("Varible Importance")+theme_linedraw()+
theme(axis.text.x = element_text(angle = 90))
pg