# Lecture 1 # Working with scalar, vector and matrix # Scalar a <- 1 b <- 2 # Operation a+b a*b # Vector a <- c(1,2) a[2] #accessing to the element of a vector b <- c(2,4) a*b # elmentwise multiplication a%*%b # scalar product # Matrix A <- matrix(c(25,7, 19, 90), nrow = 2, ncol = 2) B <- matrix(c(25,7, 19, 90), nrow = 2, ncol = 2, byrow = T) # fill by row A*B #elementwise multiplication A%*%B #matrix product (pay attention, the matrices must be conformable) ### # install and load the package #install.packages("ISLR") library(ISLR) data("Wage") # See the help help(Wage) # Exampleting working with a data frame Wage[1:2, ] # extracting first two rows Wage[c(1,100), ] # extracting first and 100 row Wage[, 1:2] # first two columns Wage[, c("year", "age")] # first two columns, but by name Wage$year # extract a variablle using the $ syntax # Structure of a dataset str(Wage) # (Some) Numerical summary summary(Wage$wage) sd(Wage$wage) IQR(Wage$wage) quantile(Wage$wage, p = 0.25) # Exploratory Analysis # Univariate boxplot(Wage$wage) # boxplot hist(Wage$wage) # histogram # Bivariate plot(Wage$age, Wage$wage, cex = 0.2) #Scatterplot boxplot(wage ~ education, data = Wage, cex.axis = 0.5) # Boxplot Y|X # Fit linear model lmFit <- lm(wage ~ age + year + education, data = Wage) lmFit summary(lmFit) ## get the quantities by hand ---- # The OLS estimate X <- model.matrix(wage ~ age + year + education, data = Wage) y <- Wage$wage beta_OLS <- solve(t(X)%*%X)%*%t(X)%*%y beta_OLS lmFit$coefficients # Std. error p <- ncol(X) hat_s2 <- sum(residuals(lmFit)^2)/(nrow(Wage) - p) hat_Vb <- hat_s2*solve(t(X)%*%X) sqrt(diag(hat_Vb)) # Predicted values and residuals head(residuals(lmFit)) head(Wage$wage - predict(lmFit)) summary(residuals(lmFit)) summary(lmFit)$sigma sqrt(sum(residuals(lmFit)^2)/(nrow(Wage)-ncol(X))) # Exercise: obtain the R^2 measures by hand # (using the formulas reported on the slides) summary(lmFit)$r.squared summary(lmFit)$adj.r.squared