This example shows how to create a Leaflet map with a basemap and an empty CartoDB.js layer. Once the layer is created a function is attached the event of the layers selector checkboxes so on any state change, the sublayers are removed and recreated according to the user selection.
https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.14/cartodb.js