GithubHelp home page GithubHelp logo

apress / advanced-r-statistical-programming-and-data-models Goto Github PK

View Code? Open in Web Editor NEW
21.0 7.0 20.0 230 KB

Source Code for 'Advanced R Statistical Programming and Data Models' by Matt Wiley and Joshua F. Wiley

License: Other

R 100.00%

advanced-r-statistical-programming-and-data-models's Introduction

Apress Source Code

This repository accompanies Advanced R Statistical Programming and Data Models by Matt Wiley and Joshua F. Wiley (Apress, 2019).

Cover image

Download the files as a zip using the green button, or clone the repository to your machine using Git.

Releases

Release v1.0 corresponds to the code in the published book, without corrections or updates.

Contributions

See the file Contributing.md for more information on how you can contribute to this repository.

advanced-r-statistical-programming-and-data-models's People

Contributors

mattwiley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

advanced-r-statistical-programming-and-data-models's Issues

Ch. 7 ML: Unsupervised, page 267: plots list filled with last ggplot only

I was unable to reproduce the results shown on page 268 using the suggested code

     p1 <- ggplot(d, aes(NYGN, SPAD))
  wgss <- vector('numeric', 8)
  plots <- vector('list', 9)

for(i in 2:9) {

 km <- kmeans(x, centers = i)
    
  wgss[i-1] <- km$tot.withinss
  plots[[i-1]] <- p1 + geom_point(aes(color = factor(km$cluster))) +
                     scale_color_viridis(discrete = TRUE) +
                     theme(legend.position = 'none') +
                     ggtitle(paste('kmeans with', i, 'centers'))

}
 plots[[9]] <- ggplot() + geom_point(aes(x = 2:9, y = wgss)) + 
                  xlab('Number of Clusters') +
                  ylab('Within SS') +
                  ggtitle('Scree Plot')

do.call(plot_grid, c(plots, ncol = 3))

As result, only the 8-th plot fills the first 8 components of plots list, with the 9- th being the scree plot:

Figure 1

Reproduction of results was possible by modifying the code as follows:

       p1 <- ggplot(d, aes(NYGN, SPAD))
    wgss <- vector('numeric', 8)
    plots <- vector('list', 9)
clusters <- vector('list', 8)


for(i in 2:9) {

 km <- kmeans(x, centers = i)
  wgss[i-1] <- km$tot.withinss
  clusters[[i-1]] <- km$cluster
 
}
scree <- ggplot() + geom_point(aes(x = 2:9, y = wgss)) + 
                  xlab('Number of Clusters') +
                  ylab('Within SS') +
                  ggtitle('Scree Plot')

plots <- lapply(2:9, 
             function(i) {p1 + 
                         geom_point(aes(color = factor(clusters[[i-1]]))) +
                         scale_color_viridis(discrete = TRUE) +
                         theme(legend.position = 'none') +
                         ggtitle(paste('kmeans with', i, 'centers'))
                             }
                      )

plots <- c(plots, list(scree))

do.call(plot_grid, c(plots, ncol = 3))

which yielded:

Figure 2

UPDATE: I have missed mapping the geom_point with aes_ in my first code example. With aes_ I was able to reproduce the results shown on page.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.