Čo znamená v R atribut triedy v structure()?

mikrom

  • ****
  • 360
    • Zobrazit profil
    • E-mail
Čo znamená v R atribut triedy v structure()?
« kdy: 12. 01. 2024, 18:46:42 »
Vazeni R-Gurus,

Potreboval som vytvoriť v R funkciu podobnú vstavanej acf() (= Auto Correlation Function).

Najprv som sa pozrel na zdroj build-in funkcie acf() pomocou prikazu

Kód: [Vybrat]
getAnywhere(plot.acf)

Potom som vytvoril svoju funkciu s názvom my_acf() s takmer rovnakou strukturou vysledkov, aku ma povodné acf(), t.j.:

Kód: [Vybrat]
> str(my_acf(x))
List of 6
 $ acf   : num [1:4, 1, 1] 1 0.25 -0.3 -0.45
 $ type  : chr "correlation"
 $ n.used: int 4
 $ lag   : num [1:4, 1, 1] 0 1 2 3
 $ series: chr "x"
 $ snames: NULL

> str(acf(x))
List of 6
 $ acf   : num [1:4, 1, 1] 1 0.25 -0.3 -0.45
 $ type  : chr "correlation"
 $ n.used: int 4
 $ lag   : num [1:4, 1, 1] 0 1 2 3
 $ series: chr "x"
 $ snames: NULL
 - attr(*, "class")= chr "acf"

Ale nepodarilo sa mi to dobre.

Zatial co povodna funkcia acf() k tomuto vstupnemu vektoru
Kód: [Vybrat]
> x <- c(1, 2, 3, 4)

vrati takyto pekny vystup

Kód: [Vybrat]
> y <- acf(x)
> y

Autocorrelations of series ‘x’, by lag

    0     1     2     3
 1.00  0.25 -0.30 -0.45

tak moja funkcia my_acf() my vratila iba nieco taketo nepekne

Kód: [Vybrat]
> y <- my_acf(x)
> y

$acf
, , 1

      [,1]
[1,]  1.00
[2,]  0.25
[3,] -0.30
[4,] -0.45


$type
[1] "correlation"

$n.used
[1] 4

$lag
, , 1

     [,1]
[1,]    0
[2,]    1
[3,]    2
[4,]    3


$series
[1] "x"

$snames
NULL

Potom som si vsimol, ze vo vysledku mojej funkcie nemam atribut class="acf"
Pridal som ho a teraz je to uz OK:

Kód: [Vybrat]
> y <- my_acf(x)
> y

Autocorrelations of series ‘x’, by lag

    0     1     2     3
 1.00  0.25 -0.30 -0.45
> y <- acf(x)
> y

Autocorrelations of series ‘x’, by lag

    0     1     2     3
 1.00  0.25 -0.30 -0.45

Teraz sa vystup mojej funkcie my_acf() zhoduje s vystupom vstavanej funkcie acf().

Ale aj tak nechapem, ako R vie, ze ma urobit speciálny vystup pre triedu „acf“.

Vie mi niekto poradit kde a akym sposobom je to nakodovane ?
« Poslední změna: 12. 01. 2024, 22:23:42 od Petr Krčmář »


mikrom

  • ****
  • 360
    • Zobrazit profil
    • E-mail
Re:Čo znamená v atribut triedy v structure()?
« Odpověď #1 kdy: 12. 01. 2024, 19:28:03 »
Kto by ste mi chceli pomoct a nemuseli vymyslat a improvizovat, tak prikladam zdrojaky co som skusal ja:

acf() bez class atributu

Kód: [Vybrat]
# How to compute acf ?
# see the book:
# Paul S.P. Cowpertwait · Andrew V. Metcalfe:
# Introductory Time Series with R
# pp 33

my_acvf <- function(x, k) {
  # acvf() - autocovariance estimation
   s = 0
   for (t in 1:(length(x) - k)) {
     s <- s + (x[t] - mean(x))*(x[t+k] - mean((x)))
   }
   return(s / (length(x) - 1))
}

my_acfk <- function(x, k) {
   #  acf() - autocorrelation estimation for given k
  return (my_acvf(x, k) / my_acvf(x, 0))
}

my_acf <- function(x) {
  #  acf() - autocorrelation estimation returns the 3D-array
  series <- deparse1(substitute(x))
  x <- as.matrix(x)
  my_acf_vector <- vector()
  my_lag_vector <- seq(from = 0, along = x)
  for (k in my_lag_vector) {
    my_acf_vector <- c(my_acf_vector, my_acfk(x, k))
  }
  my_lag_array <- array(my_lag_vector, dim = c(length(my_lag_vector), 1, 1))
  my_acf_array <- array(my_acf_vector, dim = c(length(my_acf_vector), 1, 1))
  type <- "correlation"
  sampleT <- as.integer(nrow(x))
  my_acf_out <-
    structure(
      list(acf = my_acf_array, type = type, n.used = sampleT,
           lag = my_lag_array, series = series, snames = colnames(x)))
  return (my_acf_out)
}

acf() s class atributom
Kód: [Vybrat]
# How to compute acf ?
# see the book:
# Paul S.P. Cowpertwait · Andrew V. Metcalfe:
# Introductory Time Series with R
# pp 33

my_acvf <- function(x, k) {
  # acvf() - autocovariance estimation
   s = 0
   for (t in 1:(length(x) - k)) {
     s <- s + (x[t] - mean(x))*(x[t+k] - mean((x)))
   }
   return(s / (length(x) - 1))
}

my_acfk <- function(x, k) {
   #  acf() - autocorrelation estimation for given k
  return (my_acvf(x, k) / my_acvf(x, 0))
}

my_acf <- function(x) {
  #  acf() - autocorrelation estimation returns the 3D-array
  series <- deparse1(substitute(x))
  x <- as.matrix(x)
  my_acf_vector <- vector()
  my_lag_vector <- seq(from = 0, along = x)
  for (k in my_lag_vector) {
    my_acf_vector <- c(my_acf_vector, my_acfk(x, k))
  }
  my_lag_array <- array(my_lag_vector, dim = c(length(my_lag_vector), 1, 1))
  my_acf_array <- array(my_acf_vector, dim = c(length(my_acf_vector), 1, 1))
  type <- "correlation"
  sampleT <- as.integer(nrow(x))
  my_acf_out <-
    structure(
      list(acf = my_acf_array, type = type, n.used = sampleT,
           lag = my_lag_array, series = series, snames = colnames(x)),
      class = "acf")
  return (my_acf_out)
}
« Poslední změna: 12. 01. 2024, 19:33:52 od mikrom »

alex6bbc

  • *****
  • 1 624
    • Zobrazit profil
    • E-mail
Re:Čo znamená v atribut triedy v structure()?
« Odpověď #2 kdy: 12. 01. 2024, 21:01:00 »
setting parent class?
co treba R coercion.

mikrom

  • ****
  • 360
    • Zobrazit profil
    • E-mail
Re:Čo znamená v R atribut triedy v structure()?
« Odpověď #3 kdy: 13. 01. 2024, 03:51:50 »
Myslim, ze toto je to co som na uvod potreboval vediet

Kód: [Vybrat]
> methods(class = "acf")
[1] [     plot  print
see '?methods' for accessing help and source code

> m <- methods(class = "acf")

> attr(m, "info")

> getS3method("plot", class = "acf")

> getS3method("print", class = "acf")
« Poslední změna: 13. 01. 2024, 03:53:42 od mikrom »

mikrom

  • ****
  • 360
    • Zobrazit profil
    • E-mail
Re:Čo znamená v R atribut triedy v structure()?
« Odpověď #4 kdy: 14. 01. 2024, 21:33:44 »
... nechapem, ako R vie, ze ma urobit speciálny vystup pre triedu „acf“.
ak by podobnu otazku niekto musel riesit, tak odpoved je prekvapivo jednoducha a tento clanok to dobre vysvetluje: https://www.datamentor.io/r-programming/s3-class
Podla neho som si vytvoril triedu pre vlastnu acf() a k nej metody print() a plot()