DEFINIÇÃO DE VETORES

vetor1 <- c(5:13)
print(vetor1)

vetor2 <- c(1,4,8)
print(vetor2)

vetor3 <- rep(5,9)
print(vetor3)
## [1]  5  6  7  8  9 10 11 12 13
## [1] 1 4 8
## [1] 5 5 5 5 5 5 5 5 5

OPERAÇÃO COM VETORES

t(vetor1)
5 * vetor2
vetor1 + vetor3
sum(vetor1 * vetor3)
sqrt(sum(vetor2*vetor2))
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,]    5    6    7    8    9   10   11   12   13
## [1]  5 20 40
## [1] 10 11 12 13 14 15 16 17 18
## [1] 405
## [1] 9

DEFINIÇÃO DE MATRIZES

A <- matrix(c(1:9), nrow = 3)
print(A)

b <- matrix(c(5,8,10), ncol = 1)
print(b)

C <- matrix(c(4,7,10,20,11,24,1,0,-6),nrow = 3)
print(C)
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
##      [,1]
## [1,]    5
## [2,]    8
## [3,]   10
##      [,1] [,2] [,3]
## [1,]    4   20    1
## [2,]    7   11    0
## [3,]   10   24   -6

OPERAÇÃO COM MATRIZES

A + C
A * as.vector(b)
A %*% C
t(A)
##      [,1] [,2] [,3]
## [1,]    5   24    8
## [2,]    9   16    8
## [3,]   13   30    3
##      [,1] [,2] [,3]
## [1,]    5   20   35
## [2,]   16   40   64
## [3,]   30   60   90
##      [,1] [,2] [,3]
## [1,]  102  232  -41
## [2,]  123  287  -46
## [3,]  144  342  -51
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
solve(C)
det(A)
sum(diag(C))
qr(C)$rank
##             [,1]        [,2]        [,3]
## [1,] -0.10410095  0.22712934 -0.01735016
## [2,]  0.06624606 -0.05362776  0.01104101
## [3,]  0.09148265  0.16403785 -0.15141956
## [1] 0
## [1] 9
## [1] 3

CRIAÇÃO DE SUBMATRIZES

A[2,2]
A[1,]
C[,c(1,2)]
A[c(1,3),2]
## [1] 5
## [1] 1 4 7
##      [,1] [,2]
## [1,]    4   20
## [2,]    7   11
## [3,]   10   24
## [1] 4 6

TRANSFORMAÇÃO EM DATAFRAMES

df_C <- data.frame('var1' = C[,1],
                   'var2' = C[,2],
                   'var3' = C[,3]
                   )
df_C
##   var1 var2 var3
## 1    4   20    1
## 2    7   11    0
## 3   10   24   -6

MANIPULAÇÃO DE DATAFRAMES

df_C$var1
subset(df_C, var1 > 5 )
subset(df_C, var2 %% 2 == 0 & var3 %% 2 == 0)
## [1]  4  7 10
##   var1 var2 var3
## 2    7   11    0
## 3   10   24   -6
##   var1 var2 var3
## 3   10   24   -6
Lista 5

IMPORTAÇÃO DE DADOS

library(readxl)

nome_arquivo = 'wage1.xls'
if(nome_arquivo %in% dir(getwd())){
wages <- read_excel(nome_arquivo)
print("ARQUIVO IMPORTADO")}else{ print("ARQUIVO NÃO DISPONÍVEL")}
## [1] "ARQUIVO IMPORTADO"

ESTATÍSTICAS DESCRITIVAS

paste('Média de wage: ',round(mean(wages$wage),digits = 3))

paste('Mediana de experience: ',round(median(wages$exper),digits = 3))

paste('Variância de education: ',round(var(wages$educ),digits =  3))

paste('Desvio Padrão de education: ',round(sd(wages$educ),digits=3))

paste('Covariância entre wage e education: ',round(cov(wages$wage,wages$educ),digits=3))

paste('Correlação entre wage e education: ',round(cor(wages$wage,wages$educ),digits=3))

paste('Quantil de experience com corte em 90%: ',round(quantile(wages$exper,0.9),digits = 3))
## [1] "Média de wage:  5.896"
## [1] "Mediana de experience:  13.5"
## [1] "Variância de education:  7.667"
## [1] "Desvio Padrão de education:  2.769"
## [1] "Covariância entre wage e education:  4.151"
## [1] "Correlação entre wage e education:  0.406"
## [1] "Quantil de experience com corte em 90%:  38"

FREQUÊNCIAS E DISTRIBUIÇÕES

print('Contagem das entradas com wage < 3.5 e experience = 3' )
table(wages[wages$wage<3.5 & wages$exper == 3,])
## [1] "Contagem das entradas com wage < 3.5 e experience = 3"
## , , exper = 3
## 
##       educ
## wage   9 10 11 12 13 14 15
##   0.53 0  0  0  1  0  0  0
##   2    0  1  0  1  0  0  0
##   2.95 0  0  0  1  0  0  0
##   3    1  0  1  0  1  2  0
##   3.08 0  0  0  1  0  0  0
##   3.25 0  0  0  0  0  0  1
##   3.35 0  0  0  0  0  0  1
print('Proporção de cada elemento para wages com wage < 3.5 -- sem uso prático neste caso' )
prop.table(wages$wage[wages$wage<3.5])
## [1] "Proporção de cada elemento para wages com wage < 3.5 -- sem uso prático neste caso"
##   [1] 0.007517338 0.007856831 0.007274844 0.001285222 0.007881081 0.007274844
##   [7] 0.004049663 0.007105097 0.007032349 0.003952665 0.006062370 0.007881081
##  [13] 0.008244823 0.004849896 0.007056598 0.007517338 0.007032349 0.007905330
##  [19] 0.007881081 0.007274844 0.006135118 0.008172074 0.007274844 0.007274844
##  [25] 0.007759833 0.003637422 0.007032349 0.007032349 0.005456133 0.007759833
##  [31] 0.004849896 0.005189388 0.005771376 0.007517338 0.005601630 0.006862602
##  [37] 0.007590087 0.004849896 0.005262137 0.008002328 0.007056598 0.007396091
##  [43] 0.007274844 0.006547359 0.006668607 0.007274844 0.007274844 0.007881081
##  [49] 0.008075076 0.007153596 0.007274844 0.007274844 0.007274844 0.007881081
##  [55] 0.007080848 0.007420340 0.007759833 0.007274844 0.004752898 0.007274844
##  [61] 0.008366070 0.007274844 0.007274844 0.007274844 0.007032349 0.007008099
##  [67] 0.007080848 0.007153596 0.007638586 0.004849896 0.007711334 0.007056598
##  [73] 0.007274844 0.007274844 0.007032349 0.007274844 0.007274844 0.007856831
##  [79] 0.008075076 0.007153596 0.007274844 0.007032349 0.007396091 0.008123575
##  [85] 0.007274844 0.007274844 0.007032349 0.008002328 0.008366070 0.007080848
##  [91] 0.008123575 0.007056598 0.008244823 0.007274844 0.006159368 0.007590087
##  [97] 0.007905330 0.007274844 0.006959600 0.008123575 0.007274844 0.007517338
## [103] 0.007032349 0.007080848 0.007274844 0.007032349 0.007881081 0.007759833
## [109] 0.007274844 0.007274844 0.006983850 0.008123575 0.007032349 0.007274844
## [115] 0.007274844 0.007881081 0.003637422 0.007032349 0.007978078 0.007396091
## [121] 0.007080848 0.008123575 0.007274844 0.006426112 0.007274844 0.007881081
## [127] 0.007032349 0.005407634 0.007032349 0.007953829 0.005577380 0.008002328
## [133] 0.007638586 0.007590087 0.007032349 0.004243659 0.007008099 0.007032349
## [139] 0.006304864 0.007274844 0.003467675 0.007468839 0.005504632
barplot(table(wages$educ),main = 'Gráfico de Barras de distribuição de education')

hist(wages$exper,main = 'Histograma de experience')

boxplot(wages$wage,main = 'Boxplot de wage', horizontal = TRUE)

plot(density(wages$educ), main = 'Densidade de Kernel de education')

plot(ecdf(wages$educ), main = 'Distribuição Acumulativa Empírica de experience')


CRIAÇÃO E EXPORTAÇÃO DE GRÁFICOS PERSONALIZADOS

#GRÁFICO 1
par(bg = '#b7e9ed')

plot(ecdf(wages$exper),
     xlim = c(-5,30), ylim = c(0,1),
     col = '#EC3725',lwd=1,main = 'Gráfico 1')
q <- seq(-5,30,0.1)
lines(q,exp(q)/1e9,col='dark green')

#GRÁFICO 2


plotar_graf2 <- function(){
  
par(bg = '#d7b9b2')
  
plot(wages$wage[c(1:80)],wages$educ[c(1:80)],
     xlab = 'wage', ylab = 'education', 
     cex = 1, pch = c(2,4), main = 'Gráfico 2',
     col = c('red','blue'),
     sub = 'subtítulo: Exemplo de gráfico editado')
abline(h = mean(wages$educ),lty=3)

}

#Mostra o gráfico 2
plotar_graf2()

#Salva o gráfico 2
png('Lista5_grafico2.png',width=600,height=400)
plotar_graf2()
dev.off()
## png 
##   2
#GRÁFICO 3


plotar_graf3 <- function(){
  
par(bg = '#87d992')

f <- function(x){x^2}
g <- function(x){x^3}
x_s <- seq(0,10,0.5)
  
plot(x_s,f(x_s),col='blue',ylab = 'y', xlab = 'x',main =  'Gráfico 3')
lines(x_s,g(x_s),lwd = 2,col='red',lty=4)
abline(a = 70,b=-9,lwd=2)
text(6,70,'texto aqui',col='darkgoldenrod3',font = 2)
arrows(6,65,5.15,30,lw=2,col='darkgoldenrod3')
points(5,25,col='black',pch=4,lwd=3)
legend(7.5,40,legend = c('função f','função g','função h'), col = c('blue','red','black'), cex = 0.75, lty = c(9,4,1),lwd = 2)
}

#Mostra o gráfico 3
plotar_graf3()

#Salva o gráfico 3
pdf('Lista5_grafico3.pdf',width=6,height=4)
plotar_graf3()
dev.off()
## png 
##   2

TESTES DE HIPÓTESE

print('Teste de hipótese: wage tem média maior que 5')
t.test(wages$wage,alternative = 'greater',mu=5)
## [1] "Teste de hipótese: wage tem média maior que 5"
## 
##  One Sample t-test
## 
## data:  wages$wage
## t = 5.5649, df = 525, p-value = 2.093e-08
## alternative hypothesis: true mean is greater than 5
## 95 percent confidence interval:
##  5.63077     Inf
## sample estimates:
## mean of x 
##  5.896103
print('Teste de hipótese: education tem média menor que 10 ')
t.test(wages$educ,alternative = 'less',mu=10)
## [1] "Teste de hipótese: education tem média menor que 10 "
## 
##  One Sample t-test
## 
## data:  wages$educ
## t = 21.226, df = 525, p-value = 1
## alternative hypothesis: true mean is less than 10
## 95 percent confidence interval:
##      -Inf 12.76168
## sample estimates:
## mean of x 
##  12.56274
print('Teste de hipótese: Média de experience não é 15 (conf = 90%)')
t.test(wages$exper,conf.level=0.90, alternative = 'two.sided',mu=15)
## [1] "Teste de hipótese: Média de experience não é 15 (conf = 90%)"
## 
##  One Sample t-test
## 
## data:  wages$exper
## t = 3.4086, df = 525, p-value = 0.0007032
## alternative hypothesis: true mean is not equal to 15
## 90 percent confidence interval:
##  16.04201 17.99221
## sample estimates:
## mean of x 
##  17.01711