Kamis, 17 Oktober 2019

Gadged dan Machine Learning


Melanjutkan R gadget sebelumnya, kali ini akan diberikan contoh Analisa Modeling menggunakan R programming yang mudah dibaca lewat smartphone, analisa menggunakan data  'Housing Values in Suburbs of Boston' menentukan variable yang paling berpengaruh(Variable Important) terhadap nilai perumahan.

Sebuah contoh penggunaan Machine Learning dengan R programming yang mudah dipelajari dan mudah di aplikasi. 




Seperti biasa file terdiri dari file ui dan file server, scriptnya sbb:

ui.R
 ui <-
    miniPage(
    gadgetTitleBar("Boston Analytic, simple way finding Variable Importance"),
  miniTabstripPanel(
    miniTabPanel("Data", icon = icon("table"),
      miniContentPanel(     
       dataTableOutput("data"))),
    #---keterangan
    miniTabPanel("About Boston Data", icon = icon("list-alt"),
      miniContentPanel(     
       dataTableOutput("About"),
         h3(strong('Housing Values in Suburbs of Boston')),
               # p('Housing Values in Suburbs of Boston',align = 'Justify'),
p('Description : The Boston data frame has 506 rows and 14 columns.',align = 'Justify'),
p('Usage : Boston',align = 'Justify'),
p('Format : This data frame contains the following columns:',align = 'Justify'),
p('1. crim : per capita crime rate by town.',align = 'Justify'),
p('2. zn : proportion of residential land zoned for lots over 25,000 sq.ft.',align = 'Justify'),
p('3. indus : proportion of non-retail business acres per town.',align = 'Justify'),
p('4. chas : Charles River dummy variable (= 1 if tract bounds river; 0 otherwise).',align = 'Justify'),
p('5. nox : nitrogen oxides concentration (parts per 10 million).',align = 'Justify'),
p('6. rm : average number of rooms per dwelling.',align = 'Justify'),
p('7. age : proportion of owner-occupied units built prior to 1940.',align = 'Justify'),
p('8. dis : weighted mean of distances to five Boston employment centres.',align = 'Justify'),
p('9. rad : index of accessibility to radial highways.',align = 'Justify'),
p('10. tax : full-value property-tax rate per 10,000 dollar.',align = 'Justify'),
p('11. ptratio : pupil-teacher ratio by town.',align = 'Justify'),
p('12. black : 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town.',align = 'Justify'),
p('13. lstat : lower status of the population (percent).',align = 'Justify'),
p('14. medv : median value of owner-occupied homes per 1000s dollar.',align = 'Justify'))),

    #
     miniTabPanel("Plot Model",icon = icon("area-chart"),
    miniContentPanel(
       plotOutput("model"))),

    miniTabPanel("Variable Important",icon = icon("area-chart"),
       miniContentPanel(     
        verbatimTextOutput(("var_importance")))),
   
    miniTabPanel("Variable imp",icon = icon("table"),
       miniContentPanel(     
    plotOutput("pg")))
  ))


server.R

library(shiny)
library(miniUI)
library(ggplot2)
library(dplyr)
library(MASS)
library(randomForest)

data <- Boston
data
dt1 <- data[ ,-4]
dt1
spl <- sample(nrow(dt1),nrow(dt1)*0.7)
train <- dt1[spl, ]
test <- dt1[-spl, ]

model <- randomForest(medv~., data = train)

pmod <- plot(model)
pmod+theme_linedraw()
pmod

pm <- summary(model)
pm

pv <- varImpPlot(model)
pv

var_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()
pg

server <- function(input, output, session) {
   
  output$data <- renderDataTable({
    data
  })
     output$model <- renderPlot({
    plot(model)
  })
    output$var_importance<- renderPrint({
    var_importance
  })
   output$pg <- renderPlot({
    pg
  })
    observeEvent(input$done, {
    stopApp(TRUE)
  })
}

Selanjutnya tinggal di upload di server shiny, file dibuat untuk versi gadget.

Referensi:
Kursus R programming


Tidak ada komentar:

Posting Komentar

Kapan Puncak Covid19 di Indonesia ?

Prediksi Covid19, kapan mencapai puncaknya? Selamat Idhul Fitri from Home, menjawab pertanyaan kapan covid19 mencapai puncaknya? ...