Jupyter Notebook

Jupyter has lots of cool features. In this discussion, we are getting familiar with using R and Python in Jupyter Notebook.

You can check this post for more features of Jupyter notebook.

Anaconda

Download

  • Download anaconda with Python3.8

  • Open anaconda, go to Environments, create a new enviroments call stat628 with R kernel, and Python 3.8

  • Go to home, install Jupyter notebook or Jupyter lab

  • Launch jupyter

  • If you want to use R kernel in the Jupyter Notebook, you need to install the IRkernel package in R. Instructions are here. If you are on a Mac, be sure to execute the command in R started from the Terminal, not the R App!

Now start your jupyter notebook, you can choose python or R kernel in your jupyter notebook.

library or module

In R we call library package, in python we call import module

R Python
install.packages() pip install (bash)
library(ggplot2) import pandas
ggplot2::ggplot from pandas import DataFrame
source(‘script.R’) import script
   

In R, we use install.package() in the R console to install packages. In python, we can use pip to install python packages.

# shell
pip install numpy

or

# Python
import pip
pip.main(['install', 'numpy'])

import numpy
import pandas as pd
# R
install.packages('ggplot2')
library(ggplot2)

Practice questions


Downloads

Numpy tips:

If A and B are two 2-D numpy arrays,

  • A @ B: matrix multiplication

  • X.T: transpose of a matrix

  • np.linalg.inv(A): inverse of A

  • A.shape(): returns the of rows and columns of A

  • np.diag(A): returns the diagonal elements of A

RShiny


I do not know JavaScript, you may try this if you want, but I do not recommend

The following tutorial borrows from Rstudio shiny tutorial.

You can also try this Advance shiny tips

Free server Shinyapps

  1. Install the shiny package
  2. Run the very first example

     install.packages("shiny")
     library(shiny)
     runExample("01_hello")
    

    If you notice that it require two parts, ui (how it looks like) and server (how it work inside)

Start practice

  1. create an some_random_name.R script, copy paste the following code into it. fluidPage

     library(shiny)
     ui <- fluidPage(
         titlePanel("Discussion 628")
     )
     server <- function(input, output) {}
     shinyApp(ui = ui, server = server)
    
  2. Classic Hello World. How to print a text? Always check Gallery and Reference.

     library(shiny)
     ui <- fluidPage(
         titlePanel("Discussion 628")
         ## Code you should write. No unique answer
         ## Do remember add , at the end of titlePanel("Discussion 628")
     )
     server <- function(input, output) {}
     shinyApp(ui = ui, server = server)
    
  3. Add more panels. UI layout

    1. titlePanel, sideBarPanel, mainPanel or no panel
    2. add a sidebarPanel
     ...
     ui <- fluidPage(
         titlePanel("Discussion 628"),
         ## Code you should write. No unique answer
     )
     ...
    
  4. Input data and output of data. Widget Gallery

    Based on the input give the result.

     ...
     f <- function(x) { x[1]^2 + x[2]^3 }
     ui <- fluidPage(
       titlePanel("Discussion 628"),
       ## Code you should write. No unique answer
       numericInput("n1", label = h3("Numeric input 1"), value = 5),
       numericInput("n2", label = h3("Numeric input 2"), value = 2),
       hr(),
       verbatimTextOutput("value")
     )
    
     server <- function(input, output) {
       # `value` will in the output
       output$value = renderPrint({
         f(c(input$n1, input$n2))
       })
     }
     shinyApp(ui = ui, server = server)
    
    

Practice: Create a Shiny app to calculate Fibonacci number

Mock interview question (from Google)

  • Consider Y ~ X1 + X2 and Y ~ (X1+X2) + (X1-X2) when you do OLS (assume there is no intercept). How will the predictions and the estimated regression coefficients change? How will the variance of the coefficients change? What are the intuitions?

Solution Python notebook