访问 S4 对象槽内的数据
access the data inside the slots of S4 objects
基本上我使用的是一个包,它给我一个带有许多插槽的 S4 对象。使用 s4obj@portfolio 可以轻松访问这些对象,但是我需要从这个插槽中提取一个向量。
更具体地说,包是 fPortfolio,函数是 portfolioFrontier()。在相关的 pdf 中位于第 19 页。
(http://postimg.org/image/62oa8z7dv/)
在包裹所附的 pdf 文件中指定了这样的插槽
"一个列表,包含投资组合的参数规范:
weights 指定投资组合权重的数字向量,
target返回一个指定目标的数值return, "
我想开辟 "weights" 但到目前为止没有任何效果(错误包括该对象不可子集,$ 不起作用 [意味着 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()
确实是标准的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
(尽管在您的示例中应用于命名方式的变量)。
基本上我使用的是一个包,它给我一个带有许多插槽的 S4 对象。使用 s4obj@portfolio 可以轻松访问这些对象,但是我需要从这个插槽中提取一个向量。
更具体地说,包是 fPortfolio,函数是 portfolioFrontier()。在相关的 pdf 中位于第 19 页。 (http://postimg.org/image/62oa8z7dv/)
在包裹所附的 pdf 文件中指定了这样的插槽
"一个列表,包含投资组合的参数规范: weights 指定投资组合权重的数字向量, target返回一个指定目标的数值return, "
我想开辟 "weights" 但到目前为止没有任何效果(错误包括该对象不可子集,$ 不起作用 [意味着 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()
确实是标准的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
(尽管在您的示例中应用于命名方式的变量)。