R loop for univariate rolling window on multiple variables in DF - r

I'm sure this is obvious but i'm a begginer in R and i spent a good part of the morning trying to solve this...
So, I have a DF of 24 time series of 3410 observations. I want to create a loop to procede a rolling window regression. I want to regress all the independant variable DF[,2:24] on the dependant variable DF[,1] in a rolling window and extract the evolution of the R2 in time.
I tried the following code but it isn't working:
resultsList <- list()
for (i in 1:ncol(var)) {
lmfit <- roll_regres(var[,1]~var[,i], var, 126, do_compute = c("sigmas", "r.squareds"))
resultsList[[i]] <- summary(lmfit)
}
I get the following error:
Error in roll_cpp(Y = y, X = x, window = width, do_compute_R_sqs = do_compute_R_sqs, :
'dchdd' failed with code 1
I also tried the following code using rollapply but it isn't working either:
resultsList <- list()
for (i in 1:ncol(var)) {
lmfit <- rollapply(var, width = 126, FUN = function(x) lm(var[, i] ~ var[,1]), by =1, align = "left")
resultsList[[i]] <- summary(lmfit)
}
I get the following error:
Error in zoo(rval, index(x)[i]) :
“x” : attempt to define invalid zoo object
If anyone could help me with this i would be VERY gratefull.
Thank you in advance for any help you can provide.

I solved the problem; here is the code:
#Loop RW 6 months
resultsList6M <- list()
for (i in 1:ncol(var)) {
lmfit <- roll_regres(var[,i]~cu, var, 126,do_compute = c("r.squareds"))
resultsList6M[[i]] <- lmfit["r.squareds"]
}
RS16m <- ts(as.numeric(as.character(unlist(resultsList6M[[1]]["r.squareds"]))),start = c(2007, 5), frequency = 252)
ts_plot(RS16m,
title = "126 Days RW, CADUSD ~ EURUSD ",
Xtitle = "Time",
Ytitle = "RSQUARED",
width = 1)
Now, I can do a graphical analysis of the R2 in time, i plan the repeat the rolling window on 6 months, 1 year and 3 years.

Related

Function input not recognised - local & global environment issue

I am writing a function to group together actions I regularly take on time series data. I have included all libraries I am using in the script as I think my issue may be to do with plyr / dplyr being (rightly) super specific about the environment of each variable.
The first function works great, but when getting to the second one, R doesn't recognise the input as 'x', but spits out the error: 'Error in eval(predvars, data, env) : object 'x' not found.'
Why is this happening?
library(plyr)
library(dplyr)
library(caret)
library(xts)
library(forecast)
library(imputeTS)
library(lubridate)
x1 = arima.sim(list(order = c(0,1,0)), n = 119)
timetrend <- function(x, starts, ends, frequency) {
y <- list()
y[[1]] <- ts(x, start = starts, end = ends, frequency = frequency)
y[[2]] <- decompose(y[[1]])
y[[3]] <- y[[1]] - y[[2]]$seasonal - y[[2]]$random
return(y)
}
plottime <- function(x) { #takes a timetrend list as input
t <- tslm(x[[3]] ~ trend)
plot(x[[3]])
lines(t$fitted.values)
return(t)
}
use functions from here
result <- timetrend(x = x1,
starts = c(2000, 01, 01), ends = c(2009, 12, 01), frequency = 12)
plottime(x = result)
I could make it work with the following code.
plottime <- function(x) { #takes a timetrend list as input
y=x[[3]]
t <- tslm(formula = y ~ trend)
plot(x[[3]])
lines(t$fitted.values)
return(t)
}
Not sure why it is happening, maybe the use of indexing x[[3]] in the formula argument is a problem?

Michaelis-Menten fitting by drm library: "Error in parse:"

Here is my code:
data <-data.frame(matrix(0,nrow = 9,ncol = 2))
data[,1] <- c(0,15,41,81,146,211,438,958,1733)
data[,2] <-c(0.000000,5.7013061,13.2662515,26.0874534,42.2710547,55.6050052,75.597474,112.6755999,109.45890071)
rownames(data) <- c("E0_TAP","E3_TAP","E4_TAP","E5_TAP","E6_TAP","E7_TAP","E8_TAP","E10_TAP","E12_TAP")
colnames(data) <- c('S','v')
This is the light saturation curve of photosystem II in Chlamydomonas reinhardtii. I would like to find the best fitting for my curve using the Michaelis-Menten distribution model. I tried with the drm() command in this way :
model.drm <- drm (v ~ cluster(S), data = data, fct = MM.2())
When I run this code the calculation of the fitting starts, but it's interrupted by an error that I do not really comprehend:
Error in parse(text = paste(paste(rep("c(", nrep - 1), collapse = ""), :
<text>:2:39: unexpected ')'
1: mu[(1+( 1 * (i - 1))),] %*%
2: mu[( 2 + ( 1 * (i - 1))),drop=FALSE,])
^
In addition: Warning message:
In cbind(mu[, 2:(nclass - 1)], 1) - mu[, seq(nclass - 1)] :
longer object length is not a multiple of shorter object length
Timing stopped at: 0 0 0
Although I will keep trying solve the problem by myself, I would really appreciate if someone could help me fixing it quicker or finding an alternative way to perform the analysis.
Thanks in advance!
Thanks to the help of a friend here follows the answer:
data <-data.frame(matrix(0,nrow = 9,ncol = 2))
data[,1] <- c(0,15,41,81,146,211,438,958,1733)
data[,2] <-c(0.000000,5.7013061,13.2662515,26.0874534,42.2710547,55.6050052,75.597474,112.6755999,109.45890071)
rownames(data) <- c("E0_TAP","E3_TAP","E4_TAP","E5_TAP","E6_TAP","E7_TAP","E8_TAP","E10_TAP","E12_TAP")
colnames(data) <- c('S','v')
data <- t(data) #traspose
data1 <- cbind(data,data) #duplicate
data1 <- cbind(data1,data1) # quadruplicate
data <- as.data.frame(t(data1)) #transpose
model.drm <- drm (v ~ cluster(S), data = data, fct = MM.2()) #fitting analysis
S <- data[,1]
v <- data[,2]
mml <- data.frame(S = seq(0, max(S)+9000, length.out = 200))
mml$v <- predict(model.drm, newdata = mml)
s <- mml[,1]
v <- mml[,2]
plot(s,v)
lines(s,v,lty=2,col="red",lwd=3)
coeff <- as.data.frame(coef(summary(model.drm)))
The issue comes from the dataset itself. To bypass the error, a n-uplication of my data was needed. I assume that it would be even better having more replicas of the experiment instead of cloning the selfsame.
Please leave a comment!

Error in bayesm rhierNegbinRw function:

I am attempting to fit a hierarchical negative binomial model with bayesm. Though my data is proprietary, I was able to recreate the same error with the margarine dataset. The error I get is as follows:
> look <- rhierNegbinRw(Data = list(regdata = dat1), Mcmc = list(R = 1000,
nprint = 100))
Z not specified - using a column of ones instead
Error in alpha <= 0 :
comparison (4) is possible only for atomic and list types
I set up the mock data as follows(the regression is completely nonsensical -- just trying to get the thing to work):
data(margarine)
chpr <- margarine$choicePrice
chpr$hhid <- as.factor(chpr$hhid)
N <- nlevels(chpr$hhid)
dat1 <- vector(mode = "list", length = N)
for (i in 1:N) {
dat1[[i]]$y <- chpr[chpr$hhid==levels(chpr$hhid)[i], "PPk_Stk"]
dat1[[i]]$X <- model.matrix( ~ choice + PBB_Stk,
data = chpr[chpr$hhid == levels(chpr$hhid)[i], ])
}
I would greatly appreciate any insight into this issue.

Value-at-Risk (Extreme-Value Theory) using Monte Carlo Simulation in R

I have code that successfully calculates VaR based on Extreme Value Theory using historical data. I'm trying to run this same code on multiple simulated price paths (i.e. calculating a VaR for each path) and then taking the median or average of those VaRs.
Every example I could find online had the simulation function return the price at the end of the period and then they replicated the function X many time. That makes sense to me, except that I essentially need to calculate value-at-risk for each simulated path. Below is the code I have so far. I can say that the code works when using historical data (i.e. the "evt" function works fine and the datatable is populated correctly when the lossOnly, u, and evtVar lines aren't in a function). However, I've been trying to implement simulation in the second function and trying various combinations, which have all failed.
library('RODBC')
library('nor1mix')
library('fExtremes')
library('QRM')
library('fGarch')
#function for computing the EVT VaR
evt <- function(data,u){
#fit excess returns to gpd to get estimates
gpdfit = tryCatch({
gpdfit <- gpdFit(data,u,type="mle")
}, warning = function(w) {
gpdfit <- gpdFit(data,u,type="mle",optfunc="nlminb")
return(gpdfit)
}, error = function(e) {
gpdfit <- gpdFit(data,u,type="pwm",optfunc="nlminb")
return(gpdfit)
}, finally = {})
#now calculate VaRs
xi <- gpdfit#fit$par.ests["xi"]
beta <- gpdfit#fit$par.ests["beta"]
Nu <- length(gpdfit#data$exceedances)
n <- length(data)
evtVar95 <- (u+((beta/xi)*(((n/Nu)*.05)^(-xi) - 1.)))*100
evtVar99 <- (u+((beta/xi)*(((n/Nu)*.01)^(-xi) - 1.)))*100
evtVar997 <- (u+((beta/xi)*(((n/Nu)*.003)^(-xi) - 1.)))*100
evtVar999 <- (u+((beta/xi)*(((n/Nu)*.001)^(-xi) - 1.)))*100
#return calculations
return(cbind(evtVar95,evtVar99,evtVar997,evtVar999,u,xi,beta,Nu,n))
}
#data <- read.table("pricedata.txt")
prices <- data$V1
returns <- diff(log(prices)) #or returns <- log(prices[-1]/prices[-n])
xi <- mean(returns)
std <- sd(returns)
N <- length(prices)
lstval <- prices[N]
options(scipen = 999)
p <- c(lstval, rep(NA, N-1))
gen.path <- function(){
N <- length(prices)
for(i in 2:N)
p[i] <- p[i-1] * exp(rnorm(1, xi, std))
# plot(p, type = "l", col = "brown", main = "Simulated Price")
#evt calculation
#first get only the losses and then make them absolute
lossOnly <- abs(p[p<0])
#get threshold
u <- quantile(lossOnly, probs = 0.9, names=FALSE)
evtVar <- evt(lossOnly,u)
return(evtVar)
}
runs <- 10
sim.evtVar <- replicate(runs, gen.path())
evtVar <- mean(sim.evtVar)
#add data to total table
VaR <- c(evtVar[1],evtVar[2],evtVar[3],evtVar[4],evtVar[5],evtVar[6],evtVar[7],evtVar[8],evtVar[9])
DF <- data.frame(VaR, row.names=c("evtVar95","evtVaR_99","evtVaR_997","evtVaR_999","u","xi","beta","Nu","n"))
In short, I'm trying to run the value-at-risk function (first function) within the monte carlo function (second function) and trying to put the average simulated values into a data tables. I know the first function works, but it's the second function that's driving me crazy. There are the errors I'm getting:
> sim.evtVar <- replicate(runs, gen.path())
Error in if (xi > 0.5) { : missing value where TRUE/FALSE needed
Called from: .gpdpwmFit(x, u)
Browse[1]> evtVar <- mean(sim.evtVar)
Error during wrapup: object 'sim.evtVar' not found
Browse[1]>
> #add data to total table
> VaR <- c(evtVar[1],evtVar[2],evtVar[3],evtVar[4],evtVar[5],evtVar[6],evtVar[7],evtVar[8],evtVar[9])
Error: object 'evtVar' not found
> DF <- data.frame(VaR, row.names=c("evtVar95","evtVaR_99","evtVaR_997","evtVaR_999","u","xi","beta","Nu","n"))
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ""function"" to a data.frame
Any help you can provide is greatly appreciated! Thank you in advance!
I think the Problem is this row:
lstval <- prices[N]
because if you take a stock price, that can't ever be negative, you produce an empty vector at this row in your function:
lossOnly <- abs(p[p<0])
you should try instead:
lstval <- min(returns)
if you want the highest negative return of your dataset

Error in Markov-switching VAR in R

I'm trying to estimate a Markov-switching VAR in R using the command msvar. These are the first 10 entries of my two time series. I have 798. When I try to run this I get an Error message
a <- c(1.998513, 1.995302, 2.030693, 2.122130, 2.236770, 2.314639, 2.365214, 2.455784, 2.530696, 2.596537)
b <- c(0.6421369, 0.6341437, 0.6494933, 0.6760939, 0.7113511, 0.7173038, 0.7250545, 0.7812490, 0.7874657, 0.8275209)
x <- matrix (NA,10,2)
x[,1] <- a
x[,2] <- b
time.seriesx <- ts(x)
markov.switchingx <- msvar(time.seriesx, p = 2, h = 2, niterblkopt = 10)
The error message I get is the following:
Error in optim(par = c(beta0.it), fn = llf.msar, Y = Yregmat, X =
Xregmat, : initial value in 'vmmin' is not finite
Anyone who could help me? Thanks
I think that you have to run the log-likehood function first. I get the same error, but when i did this, it works.
I'm not sure but i hope this can help you : (I used my data so don't pay attention to "M1euro")
library(base)
data <- data.matrix(M1euro, rownames.force = NA)
library(stats)
ss1<-ts(data, frequency=12, start=c(2007,1), end=c(2016,4))
class(ss1)
length(ss1)
ss <- na.approx(ss1,na.rm=F,rule=2)
ss
class(ss)
library(MSBVAR)
require(graphics)
set.seed(1)

Resources