Posts

R - Exploratory Stats

Image
  Sampling sample_n(df, 150) – Select randomly 150 obs from the dataset df %>% group_by(x) %>% sample_n(5) Select 5 obs from each x group in df Single variable geom_bar() – position = “fill” shows 100% stacked bar geom_dotplot(dotsize) stacked dots geom_density(bw) binwidth, this actually normalize the distribution with different bases to be comparable, histogram might be better to see which distribution has larger base. geom_histogram(bin) geom_boxplot(), coord_flip() to flip to horizontal box plot + xlim(c(100, 500)) set limits to x axis from 100 to 500   Multi variable facet_grid(a ~ b) Summarize() mean median sd var n – simple count IQR, inter-quartile range, range that has 50% of the data. range, total value range of the dataset Review %>% arrange(desc(x)) %>% arrange(asc(x)) ggplot facet_wrap(~ country, scales = ‘free_y’) – separate y scales Regression model <- lm(y ~ x,df) – y as explained by x summary(model) – showing coefficient, etc. but this...

R - Bayesian

Image
  prop_model(data) rbinom(n = 200 how many times to run the simulation, size = 100 sample size, prob = 0.42 probability) runif(n = 6, min = 0.0, max = 1.0), random uniform sample between 0-1, 6 samples. continuous dunif Discrete version rbeta(n_draws, shape1 = 5, shape2 = 95), generate a distribution, the larger shape1,2 the more concentrated the distribution is, larger shape1 makes the distribution closer to 1, larger shape2 makes the distribution closer to 0   Poisson distribution rpois(n_draws, lambda = mean_clicks)   Efficient alternative dbinom(x = x1, size = sample size, prob= given probability) calculates specific probability, like P( x = 10 | p = 10%). args similar to rbinom though. To generate a distribution instead of calculation one prob only, use x1 <- seq(0,100, by = 1) for example, or prob <- seq(0,1,by 0.01) expand.grid(x,y ) to generate all combination rows of 2 vectors   Normal distribution x <- data dnorm(x, mean = …, sd = …) calculate...

Stephen Grider – Modern React with Redux

Image
Section 1 install node js npm install -g create-react-app npm create-react-app app_name Semantic UI, https://semantic-ui.com/ , insert cdn script to index.html root. Section 2,3 – Basics import React from ‘react’; import ReactDOM from ‘react-dom’; const App = () => { return (div) } //functional component ReactDOM.render(<App />, document.getElementById(‘root’)); Receiving/passing props const ExpenseCard = props => { return(<div>) } <ExpenseCard abc=”” />, access by props.abc <ExpenseCard> sth else/component etc </ExpenseCard>, access by props.children Section 4,5,6 – Class-based component, States, Life Cycle Methods Class component & props as a Class extends on sth, call super() for inheritance class AssetCard extends React.Component { constructor(props) { super(props); } //life cycle methods here //other functions here render() { // can do some calculation before return return (div) } State this.state = {} // Initialization inside constructor()...

Curren – d3 js

D3 documentation https://d3js.org Javascript review for (car of cars) cars.forEach cars.map() return an array of results Working with JSON json.stringify(), convert object to json json.parse(), to convert json to object Modules export, import Asynchronous SVG graphics D3.js import {functions} from ‘d3’ const svg = select(‘svg’) , all svg tag from html .attr(‘attribute name’), extract values from html .append, add child tags to an existing tag .attr(‘attribute name’, value), add attribute and assign value It’s a good practice to group multiple components .transition Bar chart .enter .update .exit scaleLinear, for numerical data scaleBand, for nominal data, categories domain represents “data space”, range represents “display space” manage margin and innerWidth/Height with calculations

R - Markdown

Metadata — title: “” author: “” date: “” output: html_document (word,pdf,beamer/slidy_presentation) — Content # Header ## 2ndary header *italic* **bold** `code` list (blank line) * 1 (blank line) * 2 (blank line) * 3 Code inline `r x + y`, will display the result block “`{r namedBlock echo = FALSE to hide the code, eval = FALSE not run code hide code+result, result = ‘hide’, message = FALSE, warning = FALSE, error = FALSE} library(abc) code “` “`{r ref.label = ‘namedBlock’ , echo = , eval = } “` Produce output files render(“doc.rmd”, “html_document”) ggvis is only good for html document, to export pdf, there are more steps to take… So better use ggplot2 for pdf. *** to separate into new slides pandoc Select theme and extra options in metadata: output: html_document: theme: toc: true css: shiny, more interactive html Add metadata runtime: shiny

Jonas – Node.js part 2

Image
Section 10 – Auth custom validator (on model) only works on SAVE, so to update, we need to save again, not findOneandUpdate like tours password encryption: npm i bcryptjs JSONWebToken, https://github.com/auth0/node-jsonwebtoken Decode token, https://jwt.io npm i jsonwebtoken default to not display password when output, by on model, setting select: false if needs to output password, use select(‘+password’) defining extra custom function in Model, can be named anything only users can access tour info -> protect tour routes jwt.verify can receive a callback function as the last argument to run once verification is done. But to be consistent with our style, using async await so far, we can use promisify from require(‘util’) to make jwt.verify return a promise the verification here works because if there’s any error (due to unsuccessful login), it’ll be caught with catchAsync. advanced postman manually set environment variables, use by brackets {{var}} automatically set env variables i...