Führen Sie ein Makro mit Rexcel aus
Gibt es hier Rexcel-Experten? :)
Ich führe mit Rexcel in VBA ein Makro aus:
Sub create_efficient_frontier()
RInterface.StartRServer
Sheets("Analys").Range("A52:K82").ClearContents
RInterface.PutDataframe "datat", Range("ChosenData!X181:AD352")
RInterface.PutArray "startdate", Range("Analys!K2")
RInterface.PutArray "enddate", Range("Analys!K3")
RInterface.RunRFile "C:/Users/Documents/EffFront.R"
RInterface.GetDataframe "hmz$pweight", Range("Analys!A51:E76")
End Sub
Der Quell-R-Code lautet:
myfront=function(datat,startdate,enddate){
library(fPortfolio)
datat=datat[startdate:enddate,]
datanew=datat[sapply(datat[1,], function(x) !any(is.na(x)))]
datatss=as.timeSeries(datanew,datanew[,1])
Spec = portfolioSpec()
Constraints = "Long-Only"
globminhfrxmed=minvariancePortfolio(
data = datatss,
spec = Spec,
constraints = Constraints)
setNFrontierPoints(Spec) <- 25
globminfrontier <- portfolioFrontier(datatss,Spec, Constraints)
thelisttoret<-vector(mode="list")
pweights<-globminfrontier@portfolio@portfolio$weights
pweights<-data.frame(pweights)
names(pweights)=colnames(globminfrontier@portfolio@portfolio$covRiskBudgets)
thelisttoret[["pweights"]]<-pweights
tret<-globminfrontier@portfolio@portfolio$targetReturn[,1,drop=FALSE]
thelisttoret[["tret"]]<-tret
trisk<-globminfrontier@portfolio@portfolio$targetRisk[,2,drop=FALSE]
thelisttoret[["trisk"]]<-trisk
return(thelisttoret)
}
hmz=myfront(datat,startdate,enddate)
Das scheint zu funktionieren, aber die erste Zeile (wo die Namen sein sollten) ist#RError
. Aber der Code funktioniert richtig in R.
Es ist seltsam, denn wenn ich online lese, heißt esRInterface.GetDataframe(varname,range) Puts the value of R variable var (which needs to be a dataframe) into Excel range range, putting variable names in the first row of the range
.
Irgendeine Idee, was ich falsch mache?
Freundliche Grüße!