基本上我使用的是一个包含许多插槽的S4对象的包。使用s4obj @ portfolio可以很容易地访问这些对象但是我需要从这个槽中提取一个向量。
更具体地说,包是fPortfolio,函数是portfolioFrontier()。相关的pdf是在第19页。 (http://postimg.org/image/62oa8z7dv/)
在附加到包的pdf中,指出这样的插槽是
“列表,包含投资组合的参数规范: 权重指定投资组合权重的数字向量, target返回一个指定目标返回值的数值,“
我想开出“权重”,但到目前为止没有任何工作(错误包括该对象不是子集,$ doens't work [意思是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()
答案 0 :(得分:4)
它实际上只是标准的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
(尽管应用于名称与你的例子相同的变量)。