简体   繁体   中英

Extract information from S4 object within another S4 object in R

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!

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(y@model.glm)))

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

简而言之,可以使用@而不是$从S4对象中提取信息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM