Vídeo#RRS10
# Clean the Global Environment
rm(list=ls())

# OLS STATISTICS
library(readxl)
elect <- read_excel("DATARV.xlsx", sheet="elect")
attach(elect)
head(elect)

# OLS Estimation 
library(stats)
reg1 <- lm(Y ~ X2 + X3 + X4 + X5 + X6)
summary(reg1)

# Add a column of 1s
elect$X1 <- 1
attach(elect)
# Matrix X
Xcomb <- cbind(X1, X2, X3, X4, X5, X6)
X <- matrix(Xcomb, 60, 6)
dim(X); X

# X Transpose
Xt <- t(X)
dim(Xt)

# Beta hat
Betahat <- solve(Xt%*%X)%*%Xt%*%Y # (X'X)^-1 X'y Equation (3.19)
Betahat
# Betahat OLS
reg1$coefficients

# OLS residuals
residuals(reg1)
round(sum(residuals(reg1)))
round(mean(residuals(reg1)))

# Plot the residuals
par(mfrow=c(1,1))
plot(residuals(reg1), type="o")

# Predicted values
yhat <- predict(reg1)
sum(yhat)
sum(elect$Y)
plot(elect$Y ~ yhat)
mean(elect$Y)
mean(yhat)

# Coefficient of determination: R^2, Equation (3.49)
TSS <- sum((Y-mean(Y))^2)
TSS
Yhat <- X %*% Betahat
ESS <- sum((Yhat-mean(Yhat))^2)
ESS
# Or...
ESS <- sum((reg1$fitted.values-mean(reg1$fitted.values))^2)
ESS
RSS <- sum((Y-Yhat)^2)
RSS
# Or...
RSS <- sum((reg1$residuals)^2); RSS <-  deviance(reg1)
RSS
R2 <- ESS/TSS
R2
R2 <- 1-RSS/TSS
R2

# Adjusted R2, Equation (3.51)
reg1[1]
k <- length(Betahat)
k
AdjR2 <- 1- (1-R2)*(length(Y)-1)/(length(Y)-k)
AdjR2
# Directly from R
summary(reg1)$r.squared 
summary(reg1)$adj.r.squared 

# Residuals variance
s2 <- RSS/(length(Y)-k) # Equation (3.29)
s <- sqrt(s2)
s
SER <- s
SER
# Directly form R
s <- summary(reg1)$sigma
s

# Variance-Covariance matrix of Betahat, Equation (3.30)
VarBetaHat <- s2*solve(Xt%*%X)
VarBetaHat
# Or...
vcov(reg1)
SEbetahat1 <- sqrt(VarBetaHat[1,1])
SEbetahat1
SEbetahat2 <- sqrt(VarBetaHat[2,2])
SEbetahat2
# Show all the SEs
summary(reg1)

# Rank and determinant of X'X matrix
reg1$rank
det(Xt%*%X)

# Extracting the standard errors from R regression
betahatse <- coef(summary(reg1))[, "Std. Error"] 
betahatse
betahatse[1]
betahatse[2]
betahatse[3]
betahatse[4]
betahatse[5]
betahatse[6]
# Or...
betahatse <- sqrt(diag(vcov(reg1)))
betahatse

# Computing F test, Equation (3.42)
F <- (ESS/(length(Betahat)-1))/(RSS/(length(Y)-length(Betahat)))
F
F <- summary(reg1)$fstatistic 
F
F[1]
# Computing the p-value associated to F
pf(F[1], F[2], F[3], lower.tail=FALSE)

# Computing the t tests, Equation (3.36)
ttest <- Betahat/betahatse
ttest
# Directly from R
ttest <- coef(summary(reg1))[, "t value"]
ttest
# Computing the p-value associated to t-test
pvalue1 <- 2*pt(abs(ttest[1]), (length(Y)-k), lower.tail=FALSE)
pvalue1
pvalue2 <- 2*pt(abs(ttest[2]), (length(Y)-k), lower.tail=FALSE)
pvalue2
summary(reg1)

# Estimating Standardized coefficients, Equation (3.52)
library(lm.beta)
reg1 <- lm(Y ~ X2 + X3 + X4 + X5 + X6, data = elect) 
summary(reg1)
betas.reg2 <- lm.beta(reg1)
print(betas.reg2)
summary(betas.reg2)

# RESET test
library(lmtest)
resettest(reg1, power=2:3, type="fitted", data=file1)

Vídeo#RRS9
rm(list=ls()) # limpa o environment

# Criação de duas variáveis a parti da função "c de combine
x <- c(10, 12, 11, 17, 14, 18, 16, 20)
y <- c(3, 5, 4, 9, 6, 8, 7, 10)

# Diagrama de dispersão com reta dos mínimos quadrados
plot(x, y, pch=16)
abline(lm(y ~ x), col="blue", lwd = 2)

# Coeficiente de correlação simples ou coeficiente de Pearson
cor(x, y)
cor.test(x, y, method = "pearson")

Vídeo#RRS7
rm(list=ls()) # limpa o environment

### REPRESENTAÇÕES GRÁFICAS 1 / GRAPHICAL REPRESENTATIONS 1
x <- round(runif(1000, 1, 10))
# Gráfico de barras
tabfreq1 <- table(x)
tabfreq1
barplot(tabfreq1, 
        main = "Cinema 'Pogues'", 
        ylab = "Número de pessoas", col = "green",
        xlab = "Número de filmes",  ylim = c(0,200))

nte1 <- rnorm(1000, 10, 2)
# Caixa de bigodes / Boxplot
boxplot(nte1, horizontal = TRUE)
boxplot(nte1, horizontal = FALSE)
# Histograma / Histogram
hist(nte1); hist(nte1, freq = TRUE)
hist(nte1); hist(nte1, freq = FALSE)

nte2 <- runif(1000, 0, 1) 
nte3 <- nte1 * nte2

par(mfrow=c(1,2))
boxplot(nte3, horizontal = TRUE)
boxplot(nte3, horizontal = FALSE)

# Diagram de dispersão / Scatterplot
par(mfrow=c(1,1))
plot(nte1, nte3)
abline(lm(nte3 ~ nte1), col="blue", lwd = 4)
abline(h=10, col="red", lwd = 4)
abline(v=12, col="green", lwd = 4)

Vídeo#RRS6
rm(list=ls()) # limpa o environment

### OPERAÇÕES COM MATRIZES / OPERATIONS INVOLVING MATRICES
# Criação de uma matrix / Matrix definition
A <- matrix(c(2, 1, 3, 1, 0, 7, 4, 5, 2), nrow=3, ncol=3, byrow=TRUE) 
A
B <- matrix(c(5, 2, 1, 0, 4, 3), nrow=3, ncol=2, byrow=FALSE)  
B  

# Caraterística de uma matriz / Rank of a matrix
qr(A)$rank; qr(B)$rank

# Transposta de uma matriz / Transpose of a matrix
t(A)

# Inversa de uma matriz / Inverse of a matrix
solve(A); solve(B)

# Traço de uma matriz / Trace of a matrix
library(psych)
tr(A)

# Determinante / Determinant
det(A); det(B)

# Multiplicação de matrizes / Product of matrices
A%*%B

# Confirme que... You can see that... B * B^-1 = I
round(A%*%solve(A))

# Valores e Vetores próprios de uma matriz
# Get the eigenvalues and eigenvectors using eigen(); 
# this returns a named list, 
# with eigenvalues named values and eigenvectors named vectors. 

ev <- eigen(A)
ev

# Extrair as componentes do output anterior / Extract components
(L <- ev$values)

det(A)
ev$values[1]*ev$values[2]*ev$values[3]

(V <- ev$vectors)

Vídeo#RRS1
3 + 4 # Trata-se da soma de 3 mais 4...
x <- 3 * 5
vetor1 <- c(8, 5, 7, 9, 10)
vetor1
(vetor1 <- c(8, 5, 7, 9, 10))
vetor1[4]
mean(vetor1)
var(vetor1)
sumsq <- function(x){return(40+x^2)}
sumsq(5)
sumsq(10)
getwd()
setwd("C:/Users/JJDC/OneDrive - ISCTE-IUL/LIVROS/LIVROS_PAPERS")
dir()
dir(pattern="*.xls")
dir(pattern="*.doc")
install.packages("readxl")
library(readxl)
familia <- read_excel("RStatistics2JDC.xlsx", sheet = "familia")
familia <- read_excel("RStatistics2JDC.xlsx", sheet = "familia")
attach(familia) # informa o R que o nome das variáveis está na primeira linha do objeto
rm(RStatistics2JDC)
head(familia)
head(familia, 3)
familia[1:6,]
familia[, 3]; familia[2:5, 2] 
mean(rendimento)
mean(familia$rendimento)
mean(rendimento)
mean(Rendimento)
view(familia)
View(familia)