Reputation: 121
I am trying to examine the clusters of some data using the kmlCov() in the kmlcov package. Upon obtaining the results, I wasn't quite sure how to extract the information from the S4 object. I think the complication is due to the S4 object in which the information is stored is actually inside another S4 object, and I am not sure how to get at it.
A partial output from the str(objectname) is as follows:
Formal class 'KmlCovList' [package "kmlcov"] with 1 slots
..@ list_part:List of 2
.. ..$ :Formal class 'GlmCluster' [package "kmlcov"] with 16 slots
.. .. .. ..@ formula :Class 'formula' length 3 Total ~ Prop
.. .. .. .. .. ..- attr(*, ".Environment")=<environment: 0x11aed66e8>
.. .. .. ..@ nClust : num 2
.. .. .. ..@ ident : chr "CaseID"
.. .. .. ..@ timeVar : chr "time"
.. .. .. ..@ time : int [1:8287] 0 1 2 3 4 5 6 7 0 1 ...
.. .. .. ..@ effectVar : chr ""
.. .. .. ..@ effect : NULL
.. .. .. ..@ model.glm :List of 30
.. .. .. .. ..$ coefficients : Named num [1:4] 0.988 2.028 0.948 6.317
.. .. .. .. .. ..- attr(*, "names")= chr [1:4] "Ga" "Gb" "Ga:Prop" "Gb:Prop"
.. .. .. .. ..$ residuals : Named num [1:8287] 4.064 0.634 -1.215 -1.936 -1.936 ...
.. .. .. .. .. ..- attr(*, "names")= chr [1:8287] "1" "2" "3" "4" ...
.. .. .. .. ..$ fitted.values : Named num [1:8287] 1.94 1.37 1.22 1.94 1.94 ...
.. .. .. .. .. ..- attr(*, "names")= chr [1:8287] "1" "2" "3" "4" ...
For example, if I type
objectname@list_part
I get everything that is stored in the object. I have tried format such as
objectname@list_part@formula
objectname@list_part$formula
and failed to get what I need. What I would like to get are specific information stored in the slots.
Any suggestions would be appreciated. Thanks!
Upvotes: 2
Views: 1171
Reputation: 994
In a nutshell, you can extract informations from S4 object with @ instead of $.
Upvotes: 0
Reputation: 5152
Following @akrun answer, you could also try:
str(res)
str(res@list_part[[1]][[1]]@model.glm)
str(res@list_part[[1]][[1]]@model.glm$formula)
res@list_part[[1]][[1]]@model.glm$formula
str(res@list_part[[1]][[2]]@ident)
str(res@list_part[[1]][[2]]@model.glm)
res@list_part[[1]][[2]]@model.glm
Upvotes: 2
Reputation: 887028
Using the example data from the ?kmlCov
library(kmlcov)
data(artifdata)
res <- kmlCov(formula = Y ~ clust(time + time2),
data = artifdata, ident = 'id',timeVar = 'time', effectVar = 'treatment',
nClust = 2:3, nRedraw = 2)
lapply(res@list_part, function(x)
lapply(x,function(y) y@formula))
#[[1]]
#[[1]][[1]]
#Y ~ time + time2
#<environment: 0x17a01718>
#[[1]][[2]]
#Y ~ time + time2
#<environment: 0x17928f80>
#[[2]]
#[[2]][[1]]
#Y ~ time + time2
#<environment: 0x26eed80>
#[[2]][[2]]
#Y ~ time + time2
#<environment: 0x1752dc70>
sapply(res@list_part, function(x) sapply(x,function(y) y@nClust))
# [,1] [,2]
#[1,] 2 3
#[2,] 2 3
lapply(res@list_part, function(x)
sapply(x,function(y) coef([email protected])))
Upvotes: 1