HELLO!
Teknik Informatika UIN MAULANA MALIK IBRAHIM MALANG
Lalu Egiq Fahalik Anggara(220605110066)
linier algebra
by Prof. Dr. Suhartono, M.Kom
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.2.3
# Creates a map centered on Melbourne
m <- leaflet() %>% setView(lng=144.9631, lat=-37.8136, zoom=10) %>% addTiles()
# Adds markers for some landmarks
m <- m %>% addMarkers(lng=144.9651, lat=-37.8202, popup="Melbourne Zoo") %>%
addMarkers(lng=144.9835, lat=-37.8190, popup="Melbourne Cricket Ground") %>%
addMarkers(lng=144.9514, lat=-37.8206, popup="Melbourne Museum") %>%
addMarkers(lng=144.9628, lat=-37.8102, popup="Melbourne Central Station") %>%
addMarkers(lng=144.8485, lat=-37.6690, popup="Melbourne Airport") %>%
addMarkers(lng=144.9671, lat=-37.8215, popup="Federation Square") %>%
addMarkers(lng=144.9710, lat=-37.8136, popup="Flinders Street Station") %>%
addMarkers(lng=144.9752, lat=-37.8140, popup="St Paul's Cathedral")
# Prints the map
m
pagos <- c(107,92,97,95,105,101,91,99,95,104)
t.test(pagos,y=NULL,alternative="two.sided",mu=100,paired=FALSE,var.equal,conf.level=0.90)
##
## One Sample t-test
##
## data: pagos
## t = -0.79888, df = 9, p-value = 0.4449
## alternative hypothesis: true mean is not equal to 100
## 90 percent confidence interval:
## 95.38755 101.81245
## sample estimates:
## mean of x
## 98.6
t <- (87.61 - 77.38)/(19.48/sqrt(18))
t
## [1] 2.22804
menyelesaikan sistem persamaan linier dari kumpulan data Guinness menggunakan Aturan Cramer dan kemudian bandingkan solusinya dengan fungsi solve() untuk melihat bagaimana mereka berbeda.
Menerapkan Cramer’s Rule secara langsung pada sistem persamaan linear dengan matriks 18x18, berikut adalah contoh implementasinya:
# Fungsi untuk menerapkan Cramer's Rule
cramers_rule <- function(A, b) {
n <- ncol(A) # Jumlah variabel
det_A <- det(A) # Determinan matriks koefisien
# Inisialisasi array untuk menyimpan solusi
x <- numeric(n)
# Solusi menggunakan Cramer's Rule
for (i in 1:n) {
A_i <- A # Duplikat matriks koefisien
A_i[, i] <- b # Mengganti kolom ke-i dengan vektor konstanta
det_A_i <- det(A_i) # Determinan matriks A_i
if (det_A != 0) {
x[i] <- det_A_i / det_A # Solusi untuk variabel ke-i
} else {
stop("Sistem tidak memiliki solusi unik.")
}
}
return(x)
}
# Contoh sistem persamaan linear dengan matriks 18x18
A <- matrix(rnorm(18*18), nrow = 18) # Matriks koefisien acak
b <- rnorm(18) # Vektor konstanta acak
# Solusi menggunakan Cramer's Rule
solution <- cramers_rule(A, b)
print(solution)
## [1] 0.36473732 0.54222669 0.89159095 -0.69813339 0.13538903 0.49282222
## [7] 0.05127421 -0.13846515 0.70986638 -0.50937915 0.47443888 0.38116229
## [13] -1.22568318 0.01084397 -0.78370744 1.09846950 0.65405128 -0.63006083
Pada contoh di atas, kita menggunakan fungsi rnorm() untuk menghasilkan matriks koefisien acak dengan ukuran 18x18 dan vektor konstanta acak dengan panjang 18. Kemudian, kita memanggil fungsi cramers_rule() dengan matriks koefisien A dan vektor konstanta b sebagai argumen. Fungsi ini menghitung determinan matriks koefisien det_A dan menggunakan loop untuk menghitung solusi menggunakan Cramer’s Rule.
contoh lain yang menggunakan Cramer’s Rule dengan matriks 4x4 dalam R:
# Fungsi untuk menerapkan Cramer's Rule
cramers_rule <- function(A, b) {
n <- ncol(A) # Jumlah variabel
det_A <- det(A) # Determinan matriks koefisien
# Inisialisasi array untuk menyimpan solusi
x <- numeric(n)
# Solusi menggunakan Cramer's Rule
for (i in 1:n) {
A_i <- A # Duplikat matriks koefisien
A_i[, i] <- b # Mengganti kolom ke-i dengan vektor konstanta
det_A_i <- det(A_i) # Determinan matriks A_i
if (det_A != 0) {
x[i] <- det_A_i / det_A # Solusi untuk variabel ke-i
} else {
stop("Sistem tidak memiliki solusi unik.")
}
}
return(x)
}
# Contoh sistem persamaan linear dengan matriks 4x4
A <- matrix(c(2, 1, 1, -1,
3, 2, -1, 2,
-2, 3, 2, -1,
1, -1, 3, -2), nrow = 4, byrow = TRUE) # Matriks koefisien
b <- c(4, 1, 3, -1) # Vektor konstanta
# Solusi menggunakan Cramer's Rule
solution <- cramers_rule(A, b)
print(solution)
## [1] 0.5964912 1.7192982 -2.0526316 -3.1403509
Pada contoh di atas, kita menggunakan matriks koefisien 4x4 dengan vektor konstanta yang sesuai. Kemudian, kita memanggil fungsi cramers_rule() dengan matriks koefisien A dan vektor konstanta b sebagai argumen. Fungsi ini menghitung determinan matriks koefisien det_A dan menggunakan loop untuk menghitung solusi menggunakan Cramer’s Rule.
Df <- data.frame(a = 1:10, b = rnorm(10), c = runif(10, 10, 100))
apply(Df, 1, sum) # one as second argument indicate provide sum of each row.
## [1] 25.63819 96.42662 32.04672 86.99156 22.06604 37.66882 39.23494
## [8] 52.12218 108.87456 29.89360
apply(Df, 2, sum) # provide sum of each column
## a b c
## 55.00000 0.69017 475.27308
#More example:
#Summary statistics of each column
apply(Df, 2, summary)
## a b c
## Min. 1.00 -2.7334827 15.84880
## 1st Qu. 3.25 -0.5252458 26.11769
## Median 5.50 0.3230130 30.85797
## Mean 5.50 0.0690170 47.52731
## 3rd Qu. 7.75 0.8068878 75.41245
## Max. 10.00 1.7139704 99.46758
#only mean of each column
apply(Df, 2, mean)
## a b c
## 5.500000 0.069017 47.527308
#min of each column
apply(Df, 2, min)
## a b c
## 1.000000 -2.733483 15.848803
#sum of square of each column
apply(Df, 2, function(x) sum(x^2))
## a b c
## 385.00000 14.01723 32235.84694
#Number of NA in each column
apply(Df, 2, function(x) sum(is.na(x)))
## a b c
## 0 0 0
# Ex1: Why sum of first 2 column is not returned?
#Create following R object:
Dfm <- data.frame(a = 1:10, b = 10:1, letters[1:10])
Dfm
## a b letters.1.10.
## 1 1 10 a
## 2 2 9 b
## 3 3 8 c
## 4 4 7 d
## 5 5 6 e
## 6 6 5 f
## 7 7 4 g
## 8 8 3 h
## 9 9 2 i
## 10 10 1 j
#Ex2: Get mean and variance using single line function for each column.
Df <- data.frame(a = 1:10, b = rnorm(10), c = runif(10, 10, 100))
#Ex3: Replace NA's value with 0 in following dataframe using single line function using apply
Df <- data.frame(a = c(1:9, NA), b = sample(c(1, 5, NA), size = 10, replace = TRUE))
Df
## a b
## 1 1 1
## 2 2 NA
## 3 3 NA
## 4 4 NA
## 5 5 5
## 6 6 1
## 7 7 NA
## 8 8 5
## 9 9 NA
## 10 NA 5
lapply apply a Function to each element of a List or Vector. Lapply returns output in the list, of the same length of X (input vector or list).
v <- c(1, 2, 3, 4, 5)
#square root of each element
lapply(v, sqrt)
## [[1]]
## [1] 1
##
## [[2]]
## [1] 1.414214
##
## [[3]]
## [1] 1.732051
##
## [[4]]
## [1] 2
##
## [[5]]
## [1] 2.236068
#square of each element
lapply(v, function(x) x^2)
## [[1]]
## [1] 1
##
## [[2]]
## [1] 4
##
## [[3]]
## [1] 9
##
## [[4]]
## [1] 16
##
## [[5]]
## [1] 25
#Function on list
Ls <- list(a = 1:10, b = runif(5), c = rnorm(15))
#length of each element
lapply(Ls, length)
## $a
## [1] 10
##
## $b
## [1] 5
##
## $c
## [1] 15
#mean of each element
lapply(Ls, mean)
## $a
## [1] 5.5
##
## $b
## [1] 0.3492414
##
## $c
## [1] -0.1918157
#summary statistics of each element of list
lapply(Ls, summary)
## $a
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 3.25 5.50 5.50 7.75 10.00
##
## $b
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.07214 0.14380 0.14740 0.34924 0.60848 0.77439
##
## $c
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -1.4572 -1.0720 -0.2290 -0.1918 0.6000 1.4858
#Ex1: Why following code works on dataframe?
Df <- data.frame(a = 1:10, b = 10:1, c = rnorm(10))
lapply(Df, mean)
## $a
## [1] 5.5
##
## $b
## [1] 5.5
##
## $c
## [1] 0.006945276
#Ex2: calculate number of missing values for each column of data in list (Lsd).
data(cars)
data("mtcars")
Lsd <- list(cars = cars, mtcars = mtcars)
Lsd
## $cars
## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
## 7 10 18
## 8 10 26
## 9 10 34
## 10 11 17
## 11 11 28
## 12 12 14
## 13 12 20
## 14 12 24
## 15 12 28
## 16 13 26
## 17 13 34
## 18 13 34
## 19 13 46
## 20 14 26
## 21 14 36
## 22 14 60
## 23 14 80
## 24 15 20
## 25 15 26
## 26 15 54
## 27 16 32
## 28 16 40
## 29 17 32
## 30 17 40
## 31 17 50
## 32 18 42
## 33 18 56
## 34 18 76
## 35 18 84
## 36 19 36
## 37 19 46
## 38 19 68
## 39 20 32
## 40 20 48
## 41 20 52
## 42 20 56
## 43 20 64
## 44 22 66
## 45 23 54
## 46 24 70
## 47 24 92
## 48 24 93
## 49 24 120
## 50 25 85
##
## $mtcars
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
Sapply is same as lapply, but it simplifies the output to vector or matrix, if possible. sapply(x, f, simplify = FALSE, USE.NAMES = FALSE) is the same as lapply(x, f). Same example using sapply, and see the difference.
v <- c(1, 2, 3, 4, 5)
#square root of each element
sapply(v, sqrt)
## [1] 1.000000 1.414214 1.732051 2.000000 2.236068
#square of each element
sapply(v, function(x) x^2)
## [1] 1 4 9 16 25
#Function on list
Ls <- list(a = 1:10, b = runif(5), c = rnorm(15))
#length of each element
sapply(Ls, length)
## a b c
## 10 5 15
#mean of each element
sapply(Ls, mean)
## a b c
## 5.5000000 0.4791933 -0.3360318
#summary statistics of each element of list
sapply(Ls, summary) #may be much better.
## a b c
## Min. 1.00 0.3352099 -1.8576200
## 1st Qu. 3.25 0.3416651 -0.8253240
## Median 5.50 0.3469549 -0.4059331
## Mean 5.50 0.4791933 -0.3360318
## 3rd Qu. 7.75 0.6852162 0.3191446
## Max. 10.00 0.6869203 0.9300544
Attempt all excercise of lapply using sapply and check the differences in the outcome. If you find yourself typing unlist(lapply(…)), stop and consider sapply.
vapply is similar to sapply, but has a pre-specified type of return value, so it can be safer (and sometimes faster) to use. For example,
vapply(Df, FUN = mean, FUN.VALUE = 0) #since return values are numeric, it works.
## a b c
## 5.500000000 5.500000000 0.006945276
This is useful when you have several data structures (e.g. vectors, lists) and you want to apply a function to the 1st elements of each, and then the 2nd elements of each, etc., coercing the result to a vector/array as in sapply.
#Sums the 1st elements, the 2nd elements, etc.
mapply(sum, 1:5, 1:5, 1:5)
## [1] 3 6 9 12 15
mapply(rep, 1:4, 4:1)
## [[1]]
## [1] 1 1 1 1
##
## [[2]]
## [1] 2 2 2
##
## [[3]]
## [1] 3 3
##
## [[4]]
## [1] 4
#To generate random numbers with different mean and standard deviation
mapply(FUN = function(x, y) rnorm(5, x, y), 1:5, 5:1)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5.270520 0.29908733 -0.9030790 1.09110297 4.307928
## [2,] -4.160215 0.02229367 3.1959016 2.66567256 4.914663
## [3,] -3.527955 -1.35385325 4.0393162 3.18957477 4.824926
## [4,] -3.146810 0.46259669 0.4675061 3.49284552 5.405164
## [5,] 4.678336 -4.02253110 1.7951489 -0.08771228 4.391113
I never felt the need of mapply function for any of the problem.