Some Git exercise

  1. create a git repository in our course organization, you may follow this tutorial.

  1. Now let’s create a local copy of your repository.

    1. Enter your repository on GitHub by clicking on it (Don’t click on my repository…).

    2. Now get your git repository address by clicking on “Code” button then copy the url.

    3. Open locate the “Terminal” tab in your RStudio app.

      • For Mac/Linux users, you may open the “terminal” that ships with the Operating System for this.

      • For Windows users, you should see something similar to the figure below. If your “terminal” failed on initiation, try “New Terminal” (hotkey combination: Alt+Shift+R). This creates a Linux shell (so that it uses the same commands as in a Linux command-line environment) on Windows for you.

    4. In the terminal window, navigate to a location that you want to store your GitHub repository that you just created. Then git clone {the_url_you_just_copied_in_step_2}. For example, I use git clone https://github.com/tulane-math7360/Math-7360-Xiang-Ji.git for my repository.

If you are not familiar with shell environment, you could start with finding where you are by type command pwd in the terminal window. You can also open the graphic file system by type open . in the terminal. If you want to change to another location, try to play with terminal using commands from the following list. Tip: cd

Some useful commands to get you started.

Manipulate files and directories

Some exercise on R functions

rm(list = ls()) # clean up workspace first

Normal equations

Now let’s implement the Normal equations from scratch. \(\hat{\beta} = (X^{\top}X)^{-1}X^{\top}Y\).

my.normal.equations <- function(X, Y) {
  if (!is.vector(Y)) {
    stop("Y is not a vector!")
  }
  
  if (!is.matrix(X)) {  # force X to be a matrix for now
    stop("X is not a matrix!")
  }
  
  if (dim(X)[1] != length(Y)) {
    stop("Dimension mismatch between X and Y!")
  }
  
  return() # finish the calculation for beta
}

set.seed(7360)
sample.size <- 100
num.col <- 2
X <- matrix(rnorm(sample.size * num.col), nrow = sample.size, ncol = num.col)
X <- cbind(1, X)
Y <- rnorm(sample.size)

system.time(result.lm <- lm(Y ~ X[, 2] + X[, 3]))
summary(result.lm)

system.time(result.my.normal.equations <- my.normal.equations(X, Y))
result.my.normal.equations

Does your result match the estimated coefficients from the lm() function?