La serie di articoli di R’s Geek Ideal copre una serie di punti chiave sulle idee, l’utilizzo, gli strumenti, le innovazioni di R, ecc. e utilizza il mio apprendimento e la mia esperienza personali per spiegare la potenza di R.
Come linguaggio statistico, il linguaggio R ha sempre brillato in campi di nicchia. Fino all’esplosione dei big data, il linguaggio R è diventato uno strumento importante per l’analisi dei dati. Man mano che sempre più persone con un background in ingegneria si uniscono, la comunità del linguaggio R è in rapida espansione e crescita. Ora non solo il campo delle statistiche, ma anche l’istruzione, le banche, l’e-commerce, Internet… utilizzano tutti il linguaggio R.
Per diventare dei geek ideali non possiamo fermarci alla grammatica. Dobbiamo padroneggiare una solida conoscenza della matematica, della probabilità e della statistica. Dobbiamo anche avere uno spirito innovativo e utilizzare il linguaggio R in diversi ambiti. Muoviamoci insieme e diamo inizio all’ideale geek di R.
Informazioni sull’autore:
- Zhang Dan, analista di dati/programmatore/Quant: R,Java,Nodejs
- Blog: http://blog.fens.me
- E-mail: bsspirit@gmail.com
Si prega di indicare la fonte in caso di ristampa:
Prefazione
Un progetto nel tidyverse della scienza dei dati che ha ampiamente riscritto il paradigma di programmazione R, tibble ha sostituito il data.frame di base come struttura dati standard. Come complemento a tibble per le serie temporali, tsibble standardizza ulteriormente la struttura dei dati e semplifica la programmazione.
Sommario
- Introduzione a Tsibble
- installazione tsibble
- Utilizzo di base del pacchetto tsibble
1. Introduzione a Tsibble
tsibble è un progetto di estensione di tibble, utilizzato per estendere i dati delle serie temporali per formare una struttura di dati delle serie temporali standardizzata. Fare riferimento alla documentazione per il tipo di dati tibble. Scienza dei dati in linguaggio R nuovo tipo tibble
Il pacchetto tsibble fornisce una classe dati tbl_ts per rappresentare dati temporali standardizzati. Un tsibble è costituito da un indice temporale, chiavi e altre variabili di misurazione in un formato incentrato sui dati costruito sopra il tibble.
Sito ufficiale di tsibble: https://tsibble.tidyverts.org/
1.1 Indice
Il design principale di tsibble risiede nella struttura dell’indice. tsibble supporta un’ampia gamma di categorie di indici:
- Strutture temporali native del linguaggio R (come Date, POSIXct e difftime)
- tsibble nuove categorie (come annosettimana, annomese e annotrimestre)
- Altre categorie comuni: ordinato, hms::hms, lubridate::period e nanotime::nanotime
Per tbl_ts con intervalli regolari è necessario scegliere una rappresentazione dell’indice. Ad esempio, i dati mensili devono essere indicizzati utilizzando annomese, non Data o POSIXct. Poiché i mesi dell’anno garantiscono la regolarità, ogni anno ci sono 12 mesi. Tuttavia, se si utilizza Data, il numero di giorni in un mese varia tra 28 e 31, risultando in spazi temporali irregolari. Ciò vale anche per anno-settimana e anno-trimestre.
tsibble supporta qualsiasi categoria di indice purché possa essere ordinata dal passato al futuro. Per supportare le classi personalizzate, è necessario definire index_valid() per la classe e calcolare l’intervallo di tempo tramite interval_pull().
1.2 chiavi
La variabile chiave, insieme all’indice, identifica in modo univoco ciascun record:
- Chiave nulla: una variabile implicita. NULL significa produrre una serie temporale univariata.
- Variabile singola: ad esempio, data(pedestrian) utilizza il sensore come chiave.
- Variabili multiple: ad esempio, dichiarare chiave = c(Regione, Stato, Scopo) per dati(turismo). Le chiavi possono essere utilizzate insieme a selettori ordinati come inizia_con().
1.3 Intervallo di tempo:
Intervallo di tempo: la funzione intervallo restituisce l’intervallo di tempo associato a tsibble.
- Intervalli regolari: compresi i loro valori e unità di tempo: “nanosecondo”, “microsecondo”, “millisecondo”, “secondo”, “minuto”, “ora”, “giorno”, “settimana”, “quarty”, “quarty”. Gli intervalli non riconosciuti sono contrassegnati come “unità”.
- Intervalli irregolari: as_tsibble(regular = FALSE) produce tsibble irregolari, contrassegnati con !.
- Intervallo sconosciuto: contrassegnato con ? se il tsibble è vuoto o c’è solo una voce per variabile chiave.
L’intervallo di tempo si ottiene in base alla corrispondente rappresentazione dell’indice:
intervalli di tempo per operazioni di sottoinsieme come filter(), slice() e [.tbl_ts)保持不变。但是,如果结果是一个空的 tsibble,时间间隔总是未知的。当将 tsibble 与其他数据源连接并聚合到不同的时间尺度时,时间间隔会被重新计算。
1.4 时区
如果索引是 POSIXct,将显示索引对应的时区。? 表示获取到的时区是零长度字符 “”。
2. tsibble安装
tsibble包的安装,安装tsibble包非常简单,2条命令就可以完成,安装和加载。
# 安装
> install.packages("tsibble")
# 加载
> library(tsibble)
3. tsibble包的基本使用
3.1 构建年月日数据
构建一个tsibble数据集,“年月日”格式
# 加载时间包
> library(lubridate)
# 创建一个年月的时间
> mth <- make_date("2018") + months(0:3)
> mth
[1] "2018-01-01" "2018-02-01" "2018-03-01" "2018-04-01" # Costruisci tsibble: anno, mese e giorno > tsibble(mese = mese, indice = mese) # Un tsibble: 4 x 1 [1D]
luna
1 2018-01-01 2 2018-02-01 3 2018-03-01 4 2018-04-01 # Costruisci tsibble: anno e mese > tsibble(mth =annomese(mth), indice = mth) # A tsibble: bble(mth =annomese(mth), indice = mth) # A tsibble: 4 x [1M]
luna
1 gennaio 2018 febbraio 2018 3 marzo 2018 4 aprile 2018
3.2 Costruire dati di tempo, minuti e secondi
Crea un set di dati tsibble nel formato “ore, minuti e secondi”.
# 构建一个时间类型,时区为shanghai
> x <- ymd_h("2015-04-05 01", tz = "Asia/Shanghai")
# 构建tsibble的时间类型
> tsibble(time = x + (c(0, 3, 6, 9)) * 60 * 60, index = time)
# A tsibble: 4 x 1 [3h]
time
1 2015-04-05 01:00:00
2 2015-04-05 04:00:00
3 2015-04-05 07:00:00
4 2015-04-05 10:00:00
> tsibble(time = x + hours(c(0, 3, 6, 9)), index = time)
# A tsibble: 4 x 1 [3h]
time
1 2015-04-05 01:00:00
2 2015-04-05 04:00:00
3 2015-04-05 07:00:00
4 2015-04-05 10:00:00
3.3 Unione dei dati
Crea rispettivamente il set di dati tsbl1 per ore e il set di dati tsbl2 per 30 minuti, unisci tsbl1 e tsbl2 e genera il risultato in piccole unità (30 minuti).
> tsbl1 <- tsibble(
+ time = make_datetime(2018) + hours(0:3),
+ station = "A",
+ index = time, key = station
+ ) %>% print()
# A tsibble: 4 x 2 [1h]
# Key: station [1]
time station
1 2018-01-01 00:00:00 A
2 2018-01-01 01:00:00 A
3 2018-01-01 02:00:00 A
4 2018-01-01 03:00:00 A
> tsbl2 <- tsibble(
+ time = make_datetime(2018) + minutes(seq(0, 90, by = 30)),
+ station = "B",
+ index = time, key = station
+ ) %>% print()
# A tsibble: 4 x 2 [30m]
# Key: station [1]
time station
1 2018-01-01 00:00:00 B
2 2018-01-01 00:30:00 B
3 2018-01-01 01:00:00 B
4 2018-01-01 01:30:00 B
# 数据合并
> bind_rows(tsbl1, tsbl2)
# A tsibble: 8 x 2 [30m]
# Key: station [2]
time station
1 2018-01-01 00:00:00 A
2 2018-01-01 01:00:00 A
3 2018-01-01 02:00:00 A
4 2018-01-01 03:00:00 A
5 2018-01-01 00:00:00 B
6 2018-01-01 00:30:00 B
7 2018-01-01 01:00:00 B
8 2018-01-01 01:30:00 B
3.4 Conversione del tipo: da tibble a tsibble
Converti tibble in tipo tsibble.
# 创建数据tibble
> x <- make_datetime(2018) + minutes(0:1)
> tbl <- tibble(
+ time = c(x, x + minutes(15)),
+ station = rep(c("A", "B"), 2)
+ )
# 类型转换
> tbl %>%
+ mutate(time = floor_date(time, unit = "15 mins")) %>%
+ as_tsibble(index = time, key = station)
# A tsibble: 4 x 2 [15m]
# Key: station [2]
time station
1 2018-01-01 00:00:00 A
2 2018-01-01 00:15:00 A
3 2018-01-01 00:00:00 B
4 2018-01-01 00:15:00 B
3.5 Conversione del tipo: data.frame in tsibble
data.frame in tsibble.
> df<-data.frame(
+ date=as.Date("2020-01-01")+1:1000,
+ type=sample(10,1000,replace = TRUE),
+ v1=rnorm(1000),
+ v2=runif(1000,0,1)
+ )
> as_tsibble(df,index = date, key = type)
# A tsibble: 1,000 x 4 [1D]
# Key: type [10]
date type v1 v2
1 2020-01-20 1 -1.08 0.418
2 2020-02-02 1 -0.535 0.500
3 2020-02-05 1 -0.575 0.160
4 2020-02-11 1 -0.213 0.148
5 2020-03-02 1 0.876 0.0332
6 2020-03-30 1 0.872 0.475
7 2020-04-09 1 1.05 0.539
8 2020-04-10 1 -1.51 0.641
9 2020-05-10 1 -0.630 0.632
10 2020-06-12 1 -0.316 0.885
# ℹ 990 more rows
# ℹ Use `print(n = ...)` to see more rows
3.6 Confronto delle operazioni sui dati tra tibble e tsibble
Per le operazioni sui dati tibble, è necessario definire il campo group_by().
> tbl<-tibble(df)
> tbl %>%
+ group_by(type,date) %>%
+ summarise(
+ v1_high = max(v1, na.rm = TRUE),
+ v1_low = min(v1, na.rm = TRUE)
+ )
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by type and date.
ℹ Output is grouped by type.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(type, date))` for per-operation grouping instead.
# A tibble: 1,000 × 4
# Groups: type [10]
type date v1_high v1_low
1 1 2020-01-07 -0.657 -0.657
2 1 2020-01-15 1.87 1.87
3 1 2020-01-27 0.396 0.396
4 1 2020-01-28 -1.29 -1.29
5 1 2020-01-29 -1.60 -1.60
6 1 2020-02-12 0.825 0.825
7 1 2020-02-21 -0.908 -0.908
8 1 2020-02-28 -1.33 -1.33
9 1 2020-02-29 0.0145 0.0145
10 1 2020-03-06 0.177 0.177
# ℹ 990 more rows
# ℹ Use `print(n = ...)` to see more rows
Il tipo tsibble è raggruppato implicitamente tramite group_by_key()
> df_tsbl <- as_tsibble(df, key = type)
Using `date` as index variable.
> df_tsbl %>%
+ group_by_key() %>%
+ summarise(
+ v1_high = max(v1, na.rm = TRUE),
+ v1_low = min(v1, na.rm = TRUE)
+ )
# A tsibble: 1,000 x 4 [1D]
# Key: type [10]
type date v1_high v1_low
1 1 2020-01-07 -0.657 -0.657
2 1 2020-01-15 1.87 1.87
3 1 2020-01-27 0.396 0.396
4 1 2020-01-28 -1.29 -1.29
5 1 2020-01-29 -1.60 -1.60
6 1 2020-02-12 0.825 0.825
7 1 2020-02-21 -0.908 -0.908
8 1 2020-02-28 -1.33 -1.33
9 1 2020-02-29 0.0145 0.0145
10 1 2020-03-06 0.177 0.177
# ℹ 990 more rows
# ℹ Use `print(n = ...)` to see more rows
tsibble può facilmente aiutarci a organizzare i dati di tipo temporale per formare un set di dati pulito e standard, rendendo il codice del programma più basato sui dati.
Si prega di indicare la fonte in caso di ristampa:
Visualizzazioni dei post: 46
PakarPBN
A Private Blog Network (PBN) is a collection of websites that are controlled by a single individual or organization and used primarily to build backlinks to a “money site” in order to influence its ranking in search engines such as Google. The core idea behind a PBN is based on the importance of backlinks in Google’s ranking algorithm. Since Google views backlinks as signals of authority and trust, some website owners attempt to artificially create these signals through a controlled network of sites.
In a typical PBN setup, the owner acquires expired or aged domains that already have existing authority, backlinks, and history. These domains are rebuilt with new content and hosted separately, often using different IP addresses, hosting providers, themes, and ownership details to make them appear unrelated. Within the content published on these sites, links are strategically placed that point to the main website the owner wants to rank higher. By doing this, the owner attempts to pass link equity (also known as “link juice”) from the PBN sites to the target website.
The purpose of a PBN is to give the impression that the target website is naturally earning links from multiple independent sources. If done effectively, this can temporarily improve keyword rankings, increase organic visibility, and drive more traffic from search results.