Data Peubah Numerik

library(readxl)
data <- read_xlsx("C:\\Users\\Lusi Oktaviani\\OneDrive\\Statistics and Data Science\\Semester 4\\Analisis Regresi\\Anregkel4.xlsx")

y<-data$Y
xpadi<-data$Xpadi
xpdrb<-data$Xpdrb
xpmiskin<-data$Xpmiskin
xpenduduk<-data$Xpenduduk
xpengeluaran<-data$Xpengeluaran
xkpm<-data$Xkpm
xuhh<-data$Xuhh
xtpt<-data$Xtpt
xair<-data$Xair



data<-data.frame(cbind(y,xpadi,xpdrb,xpmiskin,xpenduduk,xpengeluaran,xkpm,xuhh,xtpt,xair))
head(data)
##       y    xpadi     xpdrb xpmiskin xpenduduk xpengeluaran      xkpm  xuhh xtpt
## 1 10.98 3.588756  1.505006    14.64        95      668.481 2.8157456 70.18 6.17
## 2  7.72 3.914361  2.134922     4.57       790      609.855 0.9123802 72.60 4.80
## 3  2.46 3.426600  2.344226     6.16      1310      830.111 2.7333565 70.39 3.59
## 4 11.66 3.183043  1.681262    14.62       102      626.209 0.7933535 69.69 3.59
## 5 13.48 3.271550  1.692906    11.34      1186      628.845 1.9257433 75.08 4.06
## 6  3.42 3.164308 11.467806     4.69     16158      953.321 1.2906114 73.32 7.18
##    xair
## 1 41.12
## 2 43.87
## 3 37.04
## 4 49.31
## 5 71.88
## 6 30.28

Scatter Plot

plot(xpadi,y)

plot(xpdrb,y)

plot(xpmiskin,y)

plot(xpenduduk,y)

plot(xpengeluaran,y)

plot(xkpm,y)

plot(xuhh,y)

plot(xtpt,y)

plot(xair,y)

### Korelasi

library(GGally)
## Warning: package 'GGally' was built under R version 4.3.2
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(ggplot2)

ggpairs(data,
        upper = list(continuous = wrap('cor', size = 2)),
        title = "Matriks Scatterplot Data",
        cardinality_threshold = 40)

Terlihat bahwa terdapat korelasi antara variabel peubah xpmiskin dan xpadi terhadap y (tingkat kecukupan pangan)

Matrix Plot

library(corrplot)
## Warning: package 'corrplot' was built under R version 4.3.2
## corrplot 0.92 loaded
datacor = cor(data[1:8])
corrplot(datacor, method = "number")

Visualisasi time series

library(fpp2)         # An older forecasting framework
## Warning: package 'fpp2' was built under R version 4.3.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## ── Attaching packages ────────────────────────────────────────────── fpp2 2.5 ──
## ✔ forecast  8.22.0     ✔ expsmooth 2.3   
## ✔ fma       2.5
## Warning: package 'forecast' was built under R version 4.3.3
## Warning: package 'fma' was built under R version 4.3.3
## Warning: package 'expsmooth' was built under R version 4.3.3
## 
library(fpp3)         # A newer tidy forecasting framework
## Warning: package 'fpp3' was built under R version 4.3.3
## ── Attaching packages ────────────────────────────────────────────── fpp3 0.5 ──
## ✔ tibble      3.2.1     ✔ tsibbledata 0.4.1
## ✔ dplyr       1.1.4     ✔ feasts      0.3.2
## ✔ tidyr       1.3.0     ✔ fable       0.3.4
## ✔ lubridate   1.9.3     ✔ fabletools  0.4.2
## ✔ tsibble     1.1.4
## Warning: package 'dplyr' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## Warning: package 'tsibble' was built under R version 4.3.3
## Warning: package 'tsibbledata' was built under R version 4.3.3
## Warning: package 'feasts' was built under R version 4.3.3
## Warning: package 'fabletools' was built under R version 4.3.3
## Warning: package 'fable' was built under R version 4.3.3
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date()    masks base::date()
## ✖ dplyr::filter()      masks stats::filter()
## ✖ tsibble::intersect() masks base::intersect()
## ✖ tsibble::interval()  masks lubridate::interval()
## ✖ dplyr::lag()         masks stats::lag()
## ✖ tsibble::setdiff()   masks base::setdiff()
## ✖ tsibble::union()     masks base::union()
## 
## Attaching package: 'fpp3'
## The following object is masked from 'package:fpp2':
## 
##     insurance
library(timetk)       # An even newer tidy forecasting framework
## Warning: package 'timetk' was built under R version 4.3.3
library(tidyverse)    # Collection of data manipulation tools
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'stringr' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0     ✔ readr   2.1.4
## ✔ purrr   1.0.2     ✔ stringr 1.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()     masks stats::filter()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag()        masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)    # Business Science ggplot theme
## Warning: package 'tidyquant' was built under R version 4.3.3
## Loading required package: PerformanceAnalytics
## Warning: package 'PerformanceAnalytics' was built under R version 4.3.3
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.3.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.2
## 
## Attaching package: 'zoo'
## 
## The following object is masked from 'package:tsibble':
## 
##     index
## 
## 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
## 
## 
## Attaching package: 'PerformanceAnalytics'
## 
## The following object is masked from 'package:fpp3':
## 
##     prices
## 
## The following object is masked from 'package:graphics':
## 
##     legend
## 
## Loading required package: quantmod
## Warning: package 'quantmod' was built under R version 4.3.3
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 4.3.3
## 
## Attaching package: 'tidyquant'
## 
## The following object is masked from 'package:fable':
## 
##     VAR
library(cowplot)      # A ggplot add-on for arranging plots
## 
## Attaching package: 'cowplot'
## 
## The following object is masked from 'package:lubridate':
## 
##     stamp
# Data produksi triwulanan Australia sebagai tibble
aus <- tsibbledata::aus_production %>% as_tibble()
# Check structure
aus %>% str()
## tibble [218 × 7] (S3: tbl_df/tbl/data.frame)
##  $ Quarter    : qtr [1:218] 1956 Q1, 1956 Q2, 1956 Q3, 1956 Q4, 1957 Q1, 1957 Q2, 1957...
##  $ Beer       : num [1:218] 284 213 227 308 262 228 236 320 272 233 ...
##  $ Tobacco    : num [1:218] 5225 5178 5297 5681 5577 ...
##  $ Bricks     : num [1:218] 189 204 208 197 187 214 227 222 199 229 ...
##  $ Cement     : num [1:218] 465 532 561 570 529 604 603 582 554 620 ...
##  $ Electricity: num [1:218] 3923 4436 4806 4418 4339 ...
##  $ Gas        : num [1:218] 5 6 7 6 5 7 7 6 5 7 ...
# Convert tibble to time series object
aus_prod_ts <- ts(aus[, 2:7],  #
                    start = c(1956, 1),
                    end = c(2010, 2),    
                    frequency = 4)       
aus_prod_ts %>% tail()
##         Beer Tobacco Bricks Cement Electricity Gas
## 2009 Q1  415      NA     NA   1963       58368 196
## 2009 Q2  398      NA     NA   2160       57471 238
## 2009 Q3  419      NA     NA   2325       58394 252
## 2009 Q4  488      NA     NA   2273       57336 210
## 2010 Q1  414      NA     NA   1904       58309 205
## 2010 Q2  374      NA     NA   2401       58041 236
aus_long <- aus %>% 
    rename(date = Quarter) %>%
    pivot_longer(
        cols = c("Beer","Tobacco","Bricks",
                 "Cement","Electricity","Gas"))

aus_prod_tbl_long <- aus_prod_ts %>%
  as_tsibble(index = "index",
             pivot_longer = TRUE)
# Using ggplot
aus_long %>%
    ggplot(aes(date, value, group = name, color = name)) +
    geom_line()
## Warning: Removed 44 rows containing missing values or values outside the scale range
## (`geom_line()`).

Terlihat bahwa produksi listrik naik pada triwulan pertama.

# Using fpp3
aus_prod_tbl_long %>%
  ggplot(aes(x = index, y = value, group = key)) + 
  geom_line() + 
  facet_grid(vars(key), scales = "free_y")   # With facetting
## Warning: Removed 44 rows containing missing values or values outside the scale range
## (`geom_line()`).

Data Spasial

library(sf)
## Warning: package 'sf' was built under R version 4.3.3
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(indonesia)
library(dplyr)
library(readxl)
library(ggplot2)
indonesia_kota <- id_map("indonesia", "kota")
indonesia_kota_jabar <- indonesia_kota %>% 
  filter(nama_provinsi == "Jawa Barat")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
indonesia_kota_jabar$nama_kota
##  [1] Bandung          Banjar           Bekasi           Bogor           
##  [5] Ciamis           Cianjur          Cimahi           Cirebon         
##  [9] Depok            Garut            Indramayu        Karawang        
## [13] Kota Bandung     Kota Bekasi      Kota Bogor       Kota Cirebon    
## [17] Kota Sukabumi    Kota Tasikmalaya Kuningan         Majalengka      
## [21] Purwakarta       Subang           Sukabumi         Sumedang        
## [25] Tasikmalaya     
## 432 Levels: Aceh Barat Aceh Barat Daya Aceh Besar Aceh Jaya ... Yogyakarta
data <- read_excel("C:\\Users\\Lusi Oktaviani\\OneDrive\\Statistics and Data Science\\data kerkom.xlsx")
data
## # A tibble: 25 × 2
##    Kota        Jumlah
##    <chr>        <dbl>
##  1 Bandung       2796
##  2 Banjar        1263
##  3 Bekasi        2901
##  4 Bogor         3162
##  5 Ciamis        1818
##  6 Cianjur       2016
##  7 Kota Cimahi   2123
##  8 Cirebon       2198
##  9 Kota Depok    3156
## 10 Garut         2227
## # ℹ 15 more rows
ggplot() + geom_sf(data = indonesia_kota_jabar, fill = "white", color = "black") +
  geom_sf(data = indonesia_kota_jabar, aes(fill = data$Jumlah), color = "black") +
  scale_fill_viridis_c(option = "viridis", direction = -1) + theme_void() + theme(legend.position = "right") + ggtitle("Sebaran Pengguna Internet di Provinsi Jawa barat") + theme(plot.title = element_text(face = "bold", hjust = 0.5))

Terlihat bahwa pada warna ungu di peta daerah bekasi, bogor, dan depok memiliki pengguna internet lebih dari 3000 responden.