Académique Documents
Professionnel Documents
Culture Documents
FUNGSI DALAM R
Berapa f()?
Operasi Himpunan
R memiliki fungsi-fungsi himpunan:
union(x,y)
intersect(x,y)
setdiff(x,y)
setequal(x,y)
c %in% y
choose(n,k)
combn(n,k)
Operasi Linier Aljabar
Menghitung inner product: crossprod
crossprod(1:3,c(5,12,13))
[,1]
[1,] 68
Program:
a <- matrix(c(1,-1,1,1),2,2)
b <- c(2,4)
solve(a,b)
[1] -1 3
Fungsi-fungsi Matematik
Terdapat banyak fungsi matematik:
exp(): Exponential function, base e
log(): Natural logarithm
log10(): Logarithm base 10
sqrt(): Square root
abs(): Absolute value
sin(), cos(), and so on: Trig functions
min() and max(): Minimum value and maximum value within a
vector
which.min() and which.max(): Index of the minimal element
and maximal element of a vector
Fungsi-fungsi Matematik
Lanjutan… :
pmin() and pmax(): Element-wise minima and maxima of
several vectors
sum() and prod(): Sum and product of the elements of a vector
cumsum() and cumprod(): Cumulative sum and product of the
elements of a vector
round(), floor(), and ceiling(): Round to the closest integer, to
the closest integer below, and to the closest integer above
factorial(): Factorial function
Fungsi-fungsi Matematik
exp(x^2) * (2 * x)
Kalkulus
expression({
Turunan : .expr2 <- exp(x^2)
D(expression(exp(x^2)),"x") .value <- .expr2
deriv(~exp(x^2),"x") .grad <- array(0,
c(length(.value), 1L),
eval list(NULL, c("x")))
Integral : integrate .grad[, "x"] <- .expr2
* (2 * x)
integrate(function(x) x^2,0,1)
attr(.value, "gradient")
<- .grad
.value
})
Fungsi Sebaran Statistik
Fungsi sebaran
statistik secara
konvensi:
dsebaran : density or
probability mass
function (pmf)
psebaran : cumulative
distribution function
(cdf)
qsebaran : quantiles
rsebaran : random
number generation
Optimisasi Numerik
Mencari nilai minimum/maksimum suatu fungsi
Metode:
Golden section search
Newton-Raphson
Nelder-mead
dll
Golden Section Search
Mencari nilai minimum untuk fungsi variabel tunggal dari
suatu selang yang diketahui
Misal:
Langkah:
$objective
[1] 0
optim
fr <- function(x) { $par
[1] 1.000260 1.000506
x1 <- x[1]
x2 <- x[2] $value
100 * (x2 - x1 * x1)^2 + (1 [1] 8.825241e-08
- x1)^2
} $counts
function gradient
optim(c(-1.2,1), fr)
195 NA
$convergence
[1] 0
$message
NULL
AKHIR MATERI