This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlista-1.R
137 lines (120 loc) · 5.3 KB
/
lista-1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
##
## aluno: Micael Levi L. C. - 21554923
## versão: 25-05-2021
##
################################################################################
## 1
################################################################################
A = matrix(c(0,1,3, 1,2,1, 1,1,4), nrow=3, byrow=TRUE)
B = matrix(c(0,1,2, 0,4,2, 7,0,1), nrow=3, byrow=TRUE)
## -------------------------------------------------------------------------- ##
## Resposta da 1A
## -------------------------------------------------------------------------- ##
r_1a = A %*% A - t(B) - (A + B) %*% (A - B)
# [,1] [,2] [,3]
# [1,] 32 4 -7
# [2,] 14 11 5
# [3,] 32 2 -3
## -------------------------------------------------------------------------- ##
## Resposta da 1B
## -------------------------------------------------------------------------- ##
r_1b = solve(A) %*% B + solve(B) %*% A
# [,1] [,2] [,3]
# [1,] 6.000000 -0.4047619 -0.8571429
# [2,] -3.166667 2.8333333 0.8333333
# [3,] 1.000000 -0.1666667 2.0000000
## -------------------------------------------------------------------------- ##
## Resposta da 1C
## -------------------------------------------------------------------------- ##
r_1c = det(A) * A + t( det(B) * A )
# [,1] [,2] [,3]
# [1,] 0 -48 -60
# [2,] -48 -96 -48
# [3,] -132 -48 -192
################################################################################
## 2
################################################################################
X = c(1:10, 1:11)
Z = rep(1:6,6:1)
## -------------------------------------------------------------------------- ##
## Resposta da 2A
## -------------------------------------------------------------------------- ##
y = X * Z
r_2a = (X/Z) * Z - sqrt(Z) * t(X) * y
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
# [1,] 0 -2 -6 -12 -20 -30 -131.5929 -173.0193 -220.1026 -272.8427 -331.2397 -509.6152 -411.8883
# [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
# [1,] -324.5538 -247.6115 -282 -195 -124 -97.62306 -42.72136 -13.69694
## -------------------------------------------------------------------------- ##
## Resposta da 2B
## -------------------------------------------------------------------------- ##
r_2b = ( X %*% X ) %*% t(Z) - y
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
# [1,] 890 889 888 887 886 885 1768 1766 1764 1762 1780 2667 2664 2661 2658 3540 3536 3532 4410 4405 5280
## -------------------------------------------------------------------------- ##
## Resposta da 2C
## -------------------------------------------------------------------------- ##
r_2c_1 = X[(X %% 2 == 0)]
# [1] 2 4 6 8 10 10 8 6 4 2
r_2c_2 = Z[(Z %% 2 == 0)]
# [1] 2 2 2 2 2 4 4 4 6
## -------------------------------------------------------------------------- ##
## Resposta da 2D
## -------------------------------------------------------------------------- ##
sum(X)
sum(Z)
max(X)
max(Z)
min(X)
min(Z)
sqrt(X)
sqrt(Z)
mean(X)
mean(Z)
################################################################################
## 3
################################################################################
q3 = data.frame(
c("M","M","M","M","M","F","F","F","F","F"),
c("Fundamental", "Médio", "Superior", "Superior", "Superior", "Médio", "Superior", "Superior", "Superior", "Superior"),
c(1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000)
)
colnames(q3) = c("sexo", "nivel escolar", "renda mensal")
## -------------------------------------------------------------------------- ##
## Respostas da 3
## -------------------------------------------------------------------------- ##
q3_a_1 = apply( q3[q3$`nivel escolar` == "Fundamental", ]["renda mensal"], 2, median )
q3_a_2 = apply( q3[q3$`nivel escolar` == "Médio", ]["renda mensal"], 2, median )
q3_a_3 = apply( q3[q3$`nivel escolar` == "Superior", ]["renda mensal"], 2, median )
q3_b = apply( q3[q3$sexo == "F", ]["renda mensal"], 2, median )
q3_c = apply( q3[q3$sexo == "M", ]["renda mensal"], 2, median )
q3_d = apply( q3[q3$sexo == "F" & q3$`nivel escolar` == "Superior", ]["renda mensal"], 2, median )
q3_e = apply( q3[q3$sexo == "M" & q3$`nivel escolar` == "Superior", ]["renda mensal"], 2, median )
################################################################################
## 4
################################################################################
## -------------------------------------------------------------------------- ##
## Respostas da 4
## -------------------------------------------------------------------------- ##
q4=data.frame(
stringsAsFactors=FALSE,
c( NA, 1, 2, 3),
c(1, 0.1, 0.08, 0.1),
c(2, 0.05, 0.05, 0.2),
c(3, 0.02, 0.1, 0.04),
c(4, 0.07, 0.19, 0)
)
q4[5, -1] = colSums(q4[-1,-1], na.rm=T)
q4[-1, "P.x"] = rowSums(q4[-1,-1], na.rm=T)
## melhorando a visualização do dataframe
q4[is.na(q4)]=""
q4[1,] = c("", "0", "1", "2", "3", "")
q4[,1] = c("", "0", "1", "2", "P(Y=yj)")
colnames(q4) = c("X","Y1", "Y2", "Y3", "Y4", "P(X=xi)")
View(q4)
# X Y1 Y2 Y3 Y4 P(X=xi)
# 1 0 1 2 3
# 2 0 0.1 0.05 0.02 0.07 0.24
# 3 1 0.08 0.05 0.1 0.19 0.42
# 4 2 0.1 0.2 0.04 0 0.34
# 5 P(Y=yj) 0.28 0.3 0.16 0.26 1