library(mosaicCalc)
## Loading required package: mosaic
## Registered S3 method overwritten by 'mosaic':
##   method                           from   
##   fortify.SpatialPolygonsDataFrame ggplot2
## 
## The 'mosaic' package masks several functions from core packages in order to add 
## additional features.  The original behavior of these functions should not be affected by this.
## 
## Attaching package: 'mosaic'
## The following objects are masked from 'package:dplyr':
## 
##     count, do, tally
## The following object is masked from 'package:Matrix':
## 
##     mean
## The following object is masked from 'package:ggplot2':
## 
##     stat
## The following objects are masked from 'package:stats':
## 
##     binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
##     quantile, sd, t.test, var
## The following objects are masked from 'package:base':
## 
##     max, mean, min, prod, range, sample, sum
## Loading required package: mosaicCore
## 
## Attaching package: 'mosaicCore'
## The following objects are masked from 'package:dplyr':
## 
##     count, tally
## 
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
## 
##     D

Penerapan Diferensiasi dalam Analisis Data dengan R

Diferensiasi juga memiliki aplikasi dalam analisis data. Dalam R, kita sering menggunakan diferensiasi untuk menghitung laju perubahan data atau untuk menemukan pola-pola dalam deret waktu.

Misalnya, kita ingin mencari kecepatan perubahan harga saham dari data historis:

# Mengimpor paket-paket yang diperlukan
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## ######################### Warning from 'xts' package ##########################
## #                                                                             #
## # The dplyr lag() function breaks how base R's lag() function is supposed to  #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
## # source() into this session won't work correctly.                            #
## #                                                                             #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop           #
## # dplyr from breaking base R's lag() function.                                #
## #                                                                             #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## ###############################################################################
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
# Mengunduh data historis saham Google dari Yahoo Finance
getSymbols("GOOGL", src = "yahoo")
## [1] "GOOGL"
# Mengambil kolom harga penutupan
price_data <- Cl(GOOGL)

# Menghitung turunan (perubahan) harga penutupan harian
price_diff <- diff(price_data)

# Menampilkan hasil diferensiasi
print(price_diff)
##            GOOGL.Close
## 2007-01-03          NA
## 2007-01-04  0.39214230
## 2007-01-05  0.09834862
## 2007-01-08 -0.09034061
## 2007-01-09  0.04804802
## 2007-01-10  0.09909916
## 2007-01-11  0.25675678
## 2007-01-12  0.13213253
## 2007-01-16 -0.01801777
## 2007-01-17 -0.17517662
##        ...            
## 2023-11-22  1.52000427
## 2023-11-24 -1.80000305
## 2023-11-27 -0.27999878
## 2023-11-28  0.78999329
## 2023-11-29 -2.20999146
## 2023-11-30 -2.46000671
## 2023-12-01 -0.66999817
## 2023-12-04 -2.58999634
## 2023-12-05  1.72000122
## 2023-12-06 -0.97000122

Dalam contoh ini, kita menggunakan paket quantmod untuk mengunduh data historis saham Google dari Yahoo Finance dan kemudian menghitung turunan (perubahan) harga penutupan harian menggunakan fungsi diff dari R.

Referensi: Kaplan, Daniel. 2022. MOSAIC Calculus. GitHub Pages. https://dtkaplan.github.io/MC2/