Parent and Child Classes S4
Purpose
To code S4 Classes
> setClass("Distribution", representation(domain = "character"), + prototype = prototype(domain = "Statistics")) [1] "Distribution" > a <- new("Distribution") |
Question
What happens when the prototype is a random number generator
> setClass("test", representation(val = "numeric"), prototype = prototype(val = rnorm(10))) [1] "test" > a <- new("test") > b <- new("test") > print(a) An object of class "test" Slot "val": [1] 1.3973270 -0.4279899 1.5370501 -0.2618448 -1.4394468 2.8641059 [7] 1.9870864 0.5204481 -0.7454103 -0.6267789 > print(b) An object of class "test" Slot "val": [1] 1.3973270 -0.4279899 1.5370501 -0.2618448 -1.4394468 2.8641059 [7] 1.9870864 0.5204481 -0.7454103 -0.6267789 |
Experimenting with superclass and subclass
> setClass("A_Vin", representation(s1 = "numeric"), prototype = prototype(s1 = 0)) [1] "A_Vin" > setClass("W_Vin", representation(c1 = "character")) [1] "W_Vin" > setClass("WA_Vin", contains = (c("A_Vin", "W_Vin"))) [1] "WA_Vin" > a <- new("A_Vin", s1 = 20) > b <- new("W_Vin", c1 = "Vinay") > c1 <- new("WA_Vin", b, a) > print(c1) An object of class "WA_Vin" Slot "s1": [1] 20 Slot "c1": [1] "Vinay" |
Experimenting with superclass and subclass
> setClass("A_Vin2", representation(s1 = "numeric"), prototype = prototype(s1 = 0)) [1] "A_Vin2" > setClass("W_Vin2", representation(s1 = "character")) [1] "W_Vin2" |
Initialize method - same role as init constructor
> setClass("A_Vin3", representation(s1 = "numeric", s2 = "numeric"), + prototype = prototype(s1 = 0, s2 = 0)) [1] "A_Vin3" > setMethod("initialize", "A_Vin3", function(.Object, ..., v1) { + callNextMethod(.Object, ..., s1 = v1, s2 = v1 * v1) + }) [1] "initialize" > new("A_Vin3", v1 = 9) An object of class "A_Vin3" Slot "s1": [1] 9 Slot "s2": [1] 81 |
The above initialize method has a strange syntax. But I need to remember
anyways
Create Norm_V classes and Rand_Gen Classes
> setClass("Norm_V", representation = representation(mu = "numeric", + sigma = "numeric", rsample = "numeric"), prototype = prototype(mu = 0, + sigma = 1)) [1] "Norm_V" > setClass("Rand_Gen", contains = "Norm_V", representation = representation(mu.r = "numeric", + sigma.r = "numeric", rsample.r = "numeric")) [1] "Rand_Gen" |
You can do all the jazz in the callNextMethod itself
> setMethod("initialize", "Norm_V", function(.Object, ..., mu, + sigma) { + callNextMethod(.Object, ..., mu = mu, sigma = sigma, rsample = rnorm(10, + mu, sigma)) + }) [1] "initialize" > new("Norm_V", mu = 0, sigma = 1) An object of class "Norm_V" Slot "mu": [1] 0 Slot "sigma": [1] 1 Slot "rsample": [1] 0.2159963 -0.3503482 -0.3815239 1.1360410 0.1242483 -1.9083046 [7] -1.6307656 1.1471177 -0.6211488 2.0170386 |
You can use function inside the initialize
> setMethod("initialize", "Norm_V", function(.Object, ..., mu, + sigma) { + .Object <- callNextMethod() + .Object@mu <- mu + .Object@sigma <- sigma + .Object@rsample <- rnorm(10, mu, sigma) + .Object + }) [1] "initialize" > new("Norm_V", mu = 0, sigma = 1) An object of class "Norm_V" Slot "mu": [1] 0 Slot "sigma": [1] 1 Slot "rsample": [1] -0.033404128 -1.231109141 0.351006319 0.690291731 -0.002945728 [6] -1.122482248 0.260513590 0.208631694 0.197541945 0.724580051 |
Based on the the parent class, populate sample mean, sigma
> setMethod("initialize", "Rand_Gen", function(.Object, ...) { + .Object <- callNextMethod() + .Object@mu.r <- mean(.Object@rsample) + .Object@sigma.r <- sd(.Object@rsample) + .Object@rsample.r <- rnorm(10, .Object@mu.r, .Object@sigma.r) + .Object + }) [1] "initialize" > x <- new("Rand_Gen", mu = 0, sigma = 1) > print(x) An object of class "Rand_Gen" Slot "mu.r": [1] 0.619733 Slot "sigma.r": [1] 0.7967828 Slot "rsample.r": [1] 0.83961341 -0.07846612 -0.03680434 1.68289629 0.38536194 1.64090678 [7] -0.76857785 0.85652711 1.49383104 1.45461954 Slot "mu": [1] 0 Slot "sigma": [1] 1 Slot "rsample": [1] 0.68504934 1.41442472 1.05487330 0.99589984 -0.65258381 0.07906395 [7] 0.94952952 1.42991343 1.00729814 -0.76613796 |
The above is a code where you create a class which takes mu and sd It generates a random sample stores in the parent class The subclass then calculates the sample mean and sample sd and then generates a new sample based on sample estimate