【问题标题】:access the data inside the slots of S4 objects访问 S4 对象槽内的数据
【发布时间】:2015-05-21 04:03:13
【问题描述】:

基本上我正在使用一个包,它为我提供了一个带有许多插槽的 S4 对象。使用 s4obj@portfolio 可以轻松访问这些对象,但是我需要从这个插槽中提取一个向量。

更具体地说,包是fPortfolio,函数是portfolioFrontier()。在相关的 pdf 中位于第 19 页。 (http://postimg.org/image/62oa8z7dv/)

在附在包装上的 pdf 中指定了这样的插槽是

"一个列表,包含投资组合的参数规范: weights 指定投资组合权重的数字向量, targetReturn 指定目标返回的数值,"

我想找出“权重”,但到目前为止没有任何效果(错误包括该对象不是子集,$ 不起作用 [意味着 s4obj@portfolio$weights 不起作用] 等)

工作代码:

frontier=portfolioFrontier(as.timeSeries(elements))
frontier@portfolio
frontier@portfolio$weights

Error in frontier@portfolio$weights : 
  $ operator not defined for this S4 class

p.s.:我希望这还没有在其他地方得到回答,但我在这个论坛或网络上没有找到任何关于此事的信息

p.s.2 : str() 产生

> str(frontier@portfolio)
Formal class 'fPFOLIOVAL' [package "fPortfolio"] with 2 slots
  ..@ portfolio:List of 7
  .. ..$ weights         : num [1:49, 1:14] 0.0805 0.161 0.2415 0.322 0.4025 ...
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : NULL
  .. .. .. ..$ : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. ..$ covRiskBudgets  : num [1:49, 1:14] -3.54e-05 -7.73e-05 -1.28e-04 -1.90e-04 -2.67e-04 ...
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : NULL
  .. .. .. ..$ : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. ..$ targetReturn    : num [1:49, 1:2] -0.00171 -0.00154 -0.00136 -0.00119 -0.00101 ...
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : NULL
  .. .. .. ..$ : chr [1:2] "mean" "mu"
  .. ..$ targetRisk      : num [1:49, 1:4] 0.0394 0.0359 0.0325 0.029 0.0256 ...
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : NULL
  .. .. .. ..$ : chr [1:4] "Cov" "Sigma" "CVaR" "VaR"
  .. ..$ targetAlpha     : num 0.05
  .. ..$ minriskPortfolio:Formal class 'fPORTFOLIO' [package "fPortfolio"] with 7 slots
  .. .. .. ..@ call       : language minriskPortfolio(data = data, spec = spec, constraints = constraints)
  .. .. .. ..@ data       :Formal class 'fPFOLIODATA' [package "fPortfolio"] with 3 slots
  .. .. .. .. .. ..@ data      :List of 3
  .. .. .. .. .. .. ..$ series :Time Series:          
 Name:               object
Data Matrix:        
 Dimension:          240 14
 Column Names:       MNEU OBGVEUBR OBGVEUML OBCPEU OBGVIN OBCPNOEU OBGLHGYD OBPSEM AZEU AZUS AZPC AZEM AZRE ATCOMM
 Row Names:           ... 
Positions:          
 Start:              
 End:                
With:               
 Format:             counts
 FinCenter:          
 Units:              MNEU OBGVEUBR OBGVEUML OBCPEU OBGVIN OBCPNOEU OBGLHGYD OBPSEM AZEU AZUS AZPC AZEM AZRE ATCOMM
 Title:              Signal Series Object
 Documentation:      Wed Mar 18 12:18:11 2015
  .. .. .. .. .. .. ..$ nAssets: int 14
  .. .. .. .. .. .. ..$ names  : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. ..@ statistics:List of 5
  .. .. .. .. .. .. ..$ mean     : Named num [1:14] 0.000277 0.0011 0.003337 0.002433 0.001609 ...
  .. .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. .. ..$ Cov      : num [1:14, 1:14] 2.57e-08 1.60e-07 1.39e-07 -7.50e-08 4.11e-07 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. .. .. .. ..$ : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. .. ..$ estimator: chr "covEstimator"
  .. .. .. .. .. .. ..$ mu       : Named num [1:14] 0.000277 0.0011 0.003337 0.002433 0.001609 ...
  .. .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. .. ..$ Sigma    : num [1:14, 1:14] 2.57e-08 1.60e-07 1.39e-07 -7.50e-08 4.11e-07 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. .. .. .. ..$ : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. ..@ tailRisk  : list()
  .. .. .. ..@ spec       :Formal class 'fPFOLIOSPEC' [package "fPortfolio"] with 5 slots
  .. .. .. .. .. ..@ model    :List of 5
  .. .. .. .. .. .. ..$ type     : chr "MV"
  .. .. .. .. .. .. ..$ optimize : chr "minRisk"
  .. .. .. .. .. .. ..$ estimator: chr "covEstimator"
  .. .. .. .. .. .. ..$ tailRisk : list()
  .. .. .. .. .. .. ..$ params   :List of 2
  .. .. .. .. .. .. .. ..$ alpha: num 0.05
  .. .. .. .. .. .. .. ..$ a    : num 1
  .. .. .. .. .. ..@ portfolio:List of 6
  .. .. .. .. .. .. ..$ weights        : atomic [1:14] 0.999 0 0 0 0 ...
  .. .. .. .. .. .. .. ..- attr(*, "invest")= num 1
  .. .. .. .. .. .. ..$ targetRisk     : num 0.000155
  .. .. .. .. .. .. ..$ riskFreeRate   : num 0
  .. .. .. .. .. .. ..$ nFrontierPoints: num 50
  .. .. .. .. .. .. ..$ status         : num 0
  .. .. .. .. .. .. ..$ targetReturn   : Named num -1
  .. .. .. .. .. .. .. ..- attr(*, "names")= chr ""
  .. .. .. .. .. ..@ optim    :List of 5
  .. .. .. .. .. .. ..$ solver   : chr "solveRquadprog"
  .. .. .. .. .. .. ..$ objective: chr [1:3] "portfolioObjective" "portfolioReturn" "portfolioRisk"
  .. .. .. .. .. .. ..$ options  :List of 1
  .. .. .. .. .. .. .. ..$ meq: num 2
  .. .. .. .. .. .. ..$ control  : list()
  .. .. .. .. .. .. ..$ trace    : logi FALSE
  .. .. .. .. .. ..@ messages :List of 2
  .. .. .. .. .. .. ..$ messages: logi FALSE
  .. .. .. .. .. .. ..$ note    : chr ""
  .. .. .. .. .. ..@ ampl     :List of 5
  .. .. .. .. .. .. ..$ ampl    : logi FALSE
  .. .. .. .. .. .. ..$ project : chr "ampl"
  .. .. .. .. .. .. ..$ solver  : chr "ipopt"
  .. .. .. .. .. .. ..$ protocol: logi FALSE
  .. .. .. .. .. .. ..$ trace   : logi FALSE
  .. .. .. ..@ constraints:Formal class 'fPFOLIOCON' [package "fPortfolio"] with 16 slots
  .. .. .. .. .. ..@ stringConstraints  : chr "LongOnly"
  .. .. .. .. .. ..@ minWConstraints    : Named num [1:14] 0 0 0 0 0 0 0 0 0 0 ...
  .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. ..@ maxWConstraints    : Named num [1:14] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. ..@ eqsumWConstraints  : num [1:2, 1:15] -1 -1 0.000277 -1 0.0011 ...
  .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. ..$ : chr [1:2] "Return" "Budget"
  .. .. .. .. .. .. .. ..$ : chr [1:15] "ceq" "MNEU" "OBGVEUBR" "OBGVEUML" ...
  .. .. .. .. .. ..@ minsumWConstraints : logi [1, 1] NA
  .. .. .. .. .. ..@ maxsumWConstraints : logi [1, 1] NA
  .. .. .. .. .. ..@ minBConstraints    : Named num [1:14] -Inf -Inf -Inf -Inf -Inf ...
  .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. ..@ maxBConstraints    : Named num [1:14] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. ..@ listFConstraints   : list()
  .. .. .. .. .. ..@ minFConstraints    : num(0) 
  .. .. .. .. .. ..@ maxFConstraints    : num(0) 
  .. .. .. .. .. ..@ minBuyinConstraints: Named num [1:14] 0 0 0 0 0 0 0 0 0 0 ...
  .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. ..@ maxBuyinConstraints: Named num [1:14] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. ..@ nCardConstraints   : int 14
  .. .. .. .. .. ..@ minCardConstraints : Named num [1:14] 0 0 0 0 0 0 0 0 0 0 ...
  .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. ..@ maxCardConstraints : Named num [1:14] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. ..@ portfolio  :Formal class 'fPFOLIOVAL' [package "fPortfolio"] with 2 slots
  .. .. .. .. .. ..@ portfolio:List of 6
  .. .. .. .. .. .. ..$ weights       : Named num [1:14] 0.999 0 0 0 0 ...
  .. .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. .. ..$ covRiskBudgets: Named num [1:14] 0.999 0 0 0 0 ...
  .. .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
  .. .. .. .. .. .. ..$ targetReturn  : Named num [1:2] 0.000279 0.000279
  .. .. .. .. .. .. .. ..- attr(*, "names")= chr [1:2] "mean" "mu"
  .. .. .. .. .. .. ..$ targetRisk    : Named num [1:4] 1.55e-04 1.55e-04 -1.22e-05 -3.15e-05
  .. .. .. .. .. .. .. ..- attr(*, "names")= chr [1:4] "Cov" "Sigma" "CVaR" "VaR"
  .. .. .. .. .. .. ..$ targetAlpha   : num 0.05
  .. .. .. .. .. .. ..$ status        : num 0
  .. .. .. .. .. ..@ messages : list()
  .. .. .. ..@ title      : chr "Minimum Variance Portfolio"
  .. .. .. ..@ description: chr "Wed Mar 18 12:18:11 2015 by user: dallaliberaf"
  .. ..$ status          : num 0
  ..@ messages : list()

【问题讨论】:

  • 你能告诉我们什么包,并展示你如何使用实际代码访问投资组合插槽吗?
  • s4obj@portfolio$weights 工作吗?
  • 你能告诉我们str(s4obj@portfolio)的结果吗?

标签: r object extract s4 slot


【解决方案1】:

它真的只是标准的 S4,所以你需要逐个元素地挑选。这是一个例子which I have actually wrapped in C++ as an RInside example

suppressMessages(library(fPortfolio))
lppData <- 100 * LPP2005.RET[, 1:6]
ewSpec <- portfolioSpec() 
nAssets <- ncol(lppData)

weightsvec <- c(0.5, rep(0.1, 5))
setWeights(ewSpec) <- weightsvec
ewPf <- feasiblePortfolio(data=lppData, spec=ewSpec, constraints="LongOnly")
print(ewPf)
vec <- getCovRiskBudgets(ewPf@portfolio)

在这里,您只是读取了str() 输出错误并在尝试获取权重时省略了一层@portfoio(提示:您需要两个):

R> ewPf@portfolio@portfolio$weights
SBI SPI SII LMI MPI ALT 
0.5 0.1 0.1 0.1 0.1 0.1 
R> 

这当然与我在前面的示例中给出的六个值相同。

编辑:您随后的编辑证明了这一点。你有

> str(frontier@portfolio)
Formal class 'fPFOLIOVAL' [package "fPortfolio"] with 2 slots
  ..@ portfolio:List of 7
  .. ..$ weights         : num [1:49, 1:14] 0.0805 0.161 0.2415 0.322 0.4025 ..

这正是我在代码中显示的frontier@portfolio@portfolio$weights(尽管它应用于以您的示例命名的变量)。

【讨论】:

  • 正如 Ben B. 评论的那样,str(foo@bar) 是要做的第一件事:-)
猜你喜欢
  • 1970-01-01
  • 2012-10-17
  • 2015-06-11
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 2019-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多