Quantstrat faber_rebal.R demo error - r

I have been going through the demos in quantstrat. I have a problem running faber_rebal.r. It fails with the following error:
> out<-applyStrategy.rebalancing(strategy='faber' , portfolios='faber')
Error in `colnames<-`(`*tmp*`, value = c("MaxPos", "LongLevels", "MinPos", :
length of 'dimnames' [2] not equal to array extent
Here is the output of sessionInfo():
R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_South Africa.1252 LC_CTYPE=English_South Africa.1252
[3] LC_MONETARY=English_South Africa.1252 LC_NUMERIC=C
[5] LC_TIME=English_South Africa.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] quantstrat_0.7.8 foreach_1.4.1 blotter_0.8.15
[4] PerformanceAnalytics_1.1.0 FinancialInstrument_1.1 quantmod_0.4-0
[7] Defaults_1.1-1 TTR_0.22-0 xts_0.9-5
[10] zoo_1.7-10 lattice_0.20-23
loaded via a namespace (and not attached):
[1] codetools_0.2-8 grid_3.0.1 iterators_1.0.6 tools_3.0.1
The problem occurs within the function applyStrategy.rebalancing when it calls the private function ruleProc.
I also get the same error on my Ubuntu 12.04 Machine with R 3.0.1.
Any help to get it working would be appreciated.
Thanks
Charles

I had some problems getting the faber_rebal.R demo to work as well.
First, you have to set the timezone:
ttz<-Sys.getenv('TZ')
Sys.setenv(TZ='UTC')
Second, I could get the following line in the add.rule rebalance to work:
refprice=quote(last(getPrice(mktdata)[paste('::',timestamp,sep='')][,1])),
so I changed it to:
refprice=quote(last(getPrice(mktdata)[paste('::','20140119',sep='')][,1])),
I hope that helps.
Best,
Peter

It looks like demo was fixed in the latest update to the package. Try updating to Rev.: 1595 or later

Related

Size of PDF files generated by knitr

I am using the following chunk in an Rnw file.
<<scatter,dev='pdf',include=TRUE>>=
a <- rnorm(1e4)
b <- rnorm(1e4)
plot(a,b)
#
I knitted the same Rnw file under R3.6.3 and R4.0.2, respectively. However, the PDF figure generated under R4.0.2 is much larger (~8 times) than the one from R3.6.3.
I am wondering if there is a way (eg. a chunk option) to reduce the PDF size under R4.0.2.
The session information of two different versions of R is shown below.
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 16299)
Matrix products: default
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.29
loaded via a namespace (and not attached):
[1] compiler_4.0.2 magrittr_1.5 tools_4.0.2 stringi_1.4.6 highr_0.8 stringr_1.4.0
[7] xfun_0.16 evaluate_0.14
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 16299)
Matrix products: default
Random number generation:
RNG: Mersenne-Twister
Normal: Inversion
Sample: Rounding
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.28
loaded via a namespace (and not attached):
[1] compiler_3.6.3 magrittr_1.5 tools_3.6.3 stringi_1.4.6 highr_0.8 stringr_1.4.0
[7] xfun_0.12 evaluate_0.14
The only reference to pdf in the release notes lately has been in relation to the dingbats font. Looking at the reference, it says (emphasis mine):
useDingbats logical. Should small circles be rendered via the
Dingbats font? Defaults to TRUE, which produces smaller and better
output. Setting this to FALSE can work around font display problems in
broken PDF viewers: although this font is one of the 14 guaranteed to
be available in all PDF viewers, that guarantee is not always
honoured.
You can change the default via
pdf.options(useDingbats = TRUE)
in the first code chunk of your Rnw document. This change is global to your document. If you'd rather change it only for a specific code chunk, you may use the chunk option dev.args, e.g.,
<<scatter, dev='pdf', dev.args=list(useDingbats = TRUE)>>=
a <- rnorm(1e4)
b <- rnorm(1e4)
plot(a,b)
#

switch() returns NULL in shiny on unicode characters coming from variable input only

When I use switch inside my shiny-server function on unicode characters, I don't get the expected behavior :
library(shiny)
app<-shinyApp(
ui=shinyUI(fluidPage(
selectInput("variable",label = NULL,choices = c( "a","b","é"), selected = "é"),
mainPanel(plotOutput("text"))
)),
server=function(input, output) {
output$text<-renderText({
print(switch(input$variable,
"a"="A",
"b"="B",
"é"="E"))
})
})
runApp(app)
Will print "A" in the console for an input equals to "a" but NULL in the console for input equals to "é".
On the other hand :
x<-"é"
switch(x,
"a"="A",
"b"="B",
"é"="E")
returns "E", just as I expected it.
I tried to replace the first argument of the switch call input$variable by "é" only, it does work like that. I guess input$variable is not exactly what I think it is, but coercing to a character string with as.character(input$variable) does not work either.
If I use print(input$variable) it comes up with [1] "é" which is all fine. This is beyond my current understanding...
I really need to display unicode characters and translate them in non-unicode. Any help would be greatly appreciated :-)
By the way sessionInfo() returns :
R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252
[4] LC_NUMERIC=C LC_TIME=French_France.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] shiny_0.12.0
loaded via a namespace (and not attached):
[1] digest_0.6.8 htmltools_0.2.6 httpuv_1.3.2 jsonlite_0.9.16 mime_0.3
[6] R6_2.0.1 Rcpp_0.11.6 rsconnect_0.3.79 rstudioapi_0.3.1 tools_3.1.3
[11] xtable_1.7-4

invalid or not-yet-implemented 'Matrix' subsetting in Shiny

When I run my shiny application, I got an error message saying
Error in prob[tw, uni.c] :
invalid or not-yet-implemented 'Matrix' subsetting
That same code ran without error when it was not on Shiny. Any idea how I can troubleshoot this?
I'm not sure how to reproduce the data here, but prob is of class dgCMatrix from the Matrix package, tw is a single integer, and uni.c is a numeric vector.
EDIT:
sessionInfo() output:
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_Singapore.1252 LC_CTYPE=English_Singapore.1252 LC_MONETARY=English_Singapore.1252
[4] LC_NUMERIC=C LC_TIME=English_Singapore.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] shiny_0.10.1 Matrix_1.1-4
loaded via a namespace (and not attached):
[1] bitops_1.0-6 caTools_1.17.1 digest_0.6.4 grid_3.1.1 htmltools_0.2.6 httpuv_1.3.0 lattice_0.20-29
[8] Rcpp_0.11.3 RJSONIO_1.3-0 tools_3.1.1 xtable_1.7-4
It turned out to be a bug in my code that is exposed by how Shiny works.
Outside Shiny, the function where the code resides worked seamlessly, being fed input from another function in the right format.
In Shiny, I expected the function in server.R to receive the input after the submitButton button is pressed, with sensible input keyed into the field. Apparently, even before the first press of button, the default value in the input field (which was not a sensible one) was passed to my function. That default value is not well-handled by my function and caused the error. Both changing the default value, and building extra error-checking in my function, worked to solve the issue.
Apologies for the confusion; this was a learning experience to be careful with default values and with Shiny processing sequence.

"non-numeric argument to binary operator" error from getReturns

For some reason, a code I usually run in Rstudios is no longer working. I'm hoping that someone has had a similar experience and understands what's going on.
getReturns(c('C','BAC'), start='2004-01-01', end='2008-12-31')
This results in:
Error in unclass(e1) + unclass(e2) :
non-numeric argument to binary operator
I can't find anything online nor on stackoverflow that addresses this issue. Also, I saw that the most recent documentation, from July 2014 doesn't mention anything either:
http://cran.r-project.org/web/packages/stockPortfolio/stockPortfolio.pdf
Does anyone have any idea what's going on here?
It's probably a function name clash issue. Running
timeSeries::getReturns(c('C','BAC'), start='2004-01-01', end='2008-12-31')
gives me the error, but running
stockPortfolio::getReturns(c('C','BAC'), start='2004-01-01', end='2008-12-31')
works fine.
How did this happen?
You must have loaded the stockPortfolio package, and then loaded either timeSeries or another package that depends upon timeSeries. Have a look through your console for a message that looks like
The following object is masked from ‘package:stockPortfolio’:
getReturns
Use the double colon operator (as shown above) to explicitly tell R which package to look in.
I have a similar problem using stockPortfolio in a R Markdown program.
Code that works in a R file does not work in the rmd file.
```{r p3}
recordState()
ff <- allFunds1$Fund
returns <-stockPortfolio::getReturns(ff,freq="month")
save(allFunds1,file='allFunds1.rda')
```
gives the error message and traceback
Error in unclass(e1) + unclass(e2) : non-numeric argument to binary operator
5. structure(unclass(e1) + unclass(e2), class = "Date")
4.`+.Date`(as.Date(origin, ...), x)
3. as.Date.numeric(uDates, origin = minDate)
2. as.Date(uDates, origin = minDate
1. stockPortfolio::getReturns(ff, freq = "month")
My recordState function saves the results of search() and sessionInfo() in the chunk:
[1] "search:"
[1] ".GlobalEnv" "tools:rstudio" "package:stats"
[4] "package:graphics" "package:grDevices" "package:utils"
[7] "package:datasets" "package:methods" "Autoloads"
[10] "package:base"
[1] "sessionInfo():"
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X Yosemite 10.10.5
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] Rcpp_0.12.9 digest_0.6.11 dplyr_0.5.0
[4] rprojroot_1.2 assertthat_0.1 R6_2.2.0
[7] xtable_1.8-2 DBI_0.5-1 backports_1.0.5
[10] magrittr_1.5 evaluate_0.10 stringi_1.1.2
[13] stockPortfolio_1.2 rmarkdown_1.3 tools_3.3.2
[16] stringr_1.1.0 readr_1.0.0 yaml_2.1.14
[19] htmltools_0.3.5 knitr_1.15.1 tibble_1.2
The original posting suggests that this error can result from confusing stockPortfolio::getReturns with the function in timeSeries but I have used the full name and do not have either of the libraries loaded.

R - XCMS package Error

I' ve the following problem :
Error in .C("NetCDFOpen", as.character(filename), ncid = integer(1), status = integer(1), :
C symbol name "NetCDFOpen" not in DLL for package "xcms"
How do you get this error :
nc <- xcms:::netCDFOpen(cdfFile)
ncData <- xcms:::netCDFRawData(nc)
xcms:::netCDFClose(nc)
I don't know why this don't works, although it should. For further info feel free to ask. Free .cdf files canf be found in the TargetSearchData package.
Code example :
## The directory with the NetCDF GC-MS files
cdfpath <- file.path(.find.package("TargetSearchData"), "gc-ms-data")
cdfpath
I don't think that it should, as you are implying. First, you are using a non-exported function through the :::. In addition, as stated by the error message, the is no NetCDFOpen symbol defined is the dll/so files.
Using the standard input functionality from xcms, works smoothly:
> library("xcms")
> cdfpath <- file.path(.find.package("TargetSearchData"), "gc-ms-data")
> cdfFile <- dir(cdfpath, full.names=TRUE)[1]
> xs <- xcmsSet(cdfFile)
7235eg04: 135:168 185:314 235:444 285:580
> xr <- xcmsRaw(cdfFile)
If you really want to input your data manually, you should use the functionality from the mzR package, which xcms depends on:
> openMSfile(cdfFile)
Mass Spectrometry file handle.
Filename: /home/lgatto/R/x86_64-unknown-linux-gnu-library/2.16/TargetSearchData/gc-ms-data/7235eg04.cdf
Number of scans: 4400
Finally, do pay attention to always provide the output of sessionInfo, to assure that you are using the latest version. In my case:
> sessionInfo()
R Under development (unstable) (2012-10-23 r61007)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] BiocInstaller_1.9.4 xcms_1.35.1 mzR_1.5.1
[4] Rcpp_0.9.15
loaded via a namespace (and not attached):
[1] Biobase_2.19.0 BiocGenerics_0.5.1 codetools_0.2-8 parallel_2.16.0
[5] tools_2.16.0
although if might be different for you, if you use the stable version of R and Bioconductor (currently 2.15.2/2.11).
Hope this helps.

Resources