MIDAS regression (midasr package) - r

I am trying to estimate a MIDAS regression on a subsample of my data using the window function. However, when I use this, the midas_r() function throws me back the error:
Error in prepmidas_r(y, X, mt, Zenv, cl, args, start, Ofunction, weight_gradients, :
Starting values for weight parameters must be supplied
Here is my code:
install.packages("midasr")
library(midasr)
yrs <- 10
x <- ts(rnorm(12*yrs),start=c(1900,1),frequency = 12)
y <- ts(rnorm(yrs),start=c(1900,1))
midas_r(y~fmls(x,3,12,nealmon),start=list(x=rep(0,3)))
x_est <- window(x,end=c(1910,0))
y_est <- window(y,end=(1910))
midas_r(y_est~fmls(x_est,3,12,nealmon)+1,start=list(x=rep(0,3)))
Does anyone know what's the issue? Thanks in advance!

The issue is in list(x=rep(0, 3)). This list has indeed to be named, but this name needs to coincide with the variable name. Hence,
midas_r(y_est ~ fmls(x_est, 3, 12, nealmon), start = list(x_est = rep(0, 3)))
works.

Related

SMOTE target variable not found in R

Why is it when I run the smote function in R, an error appears saying that my target variable is not found? I am using the smotefamily package to run this smote function.
tv_smote <- SMOTE(tv_smote, Churn, K = 5, dup_size = 0)
Error in table(target) : object 'Churn' not found
Chunk Codes
df data structure
df 1st few rows
Generally, you should include the minimum code and data needed to reproduce the problem. This saves time and gives you more chance of getting an answer. However, try this...
tv_smote <- SMOTE(tv_smote, tv_smote$Churn, K = 5, dup_size = 0)
I can get the same error by doing this:
library(smotefamily)
df <- data.frame(x = 1:8, y = 8:1)
df_smote <- SMOTE(df, y, K = 3, dup_size = 0)
It appears to me that SMOTE doesn't know y is a column name. In the documentation, ?SMOTE, it says that the target argument is "A vector of a target class attribute corresponding to a dataset X." My interpretation of this is that you need to supply a vector, not a name. Changing it to df_smote <- SMOTE(df, df$y, K = 3, dup_size = 0) gets past that part.
I am not familiar with the SMOTE function, but from testing it would appear that SMOTE cannot accept dataframes with any factor type columns. I get Error in get.knnx(data, query, k, algorithm) : Data non-numeric when using as.factor.

Non-linear least squares in R: Error in eval(predvars, data, env) : object not found

I am trying to fit a gnls function in R and throws me an error that says:
Error in eval(predvars, data, env) : object A not found Not sure where I am going wrong.
set.seed(111)
y.size <- rnorm(100,4,1)
p <- rnorm(100,5,1)
df <- data.frame(p, y.size)
# fit generalised nonlinear least squares
require(nlme)
mgnls <- gnls(y.size ~ ((A *((p*K/Ka)-1))-1)* log(p),
start = list(A = c(-1,-10),
K = c(800,3000),
Ka = c(35000,45000)),
data = df)
plot(mgnls) # more homogenous
For anyone needing more info: I'm trying to follow along this method
I see there are 2 issues. First I don't understand the convention list(A = c(-1,-10), K = c(800,3000),Ka = c(35000,4500)). Generally only 1 value is used to initialize the starting value.
Second, your equation defines K/Ka with both values as adjustable parameters. This will cause errors since there are an infinite number of values for K and Ka which will evaluate to the same value. It is better to set one value to a constant or define a new value equal to the ratio.
set.seed(111)
y.size <- rnorm(100,4,1)
p <- rnorm(100,5,1)
df <- data.frame(p, y.size)
# fit generalised nonlinear least squares
require(nlme)
mgnls <- gnls(y.size ~ ((A *((p*K_Ka)-1))-1)* log(p),
start = list(A = -5, K_Ka = 0.5),
data=df)
plot(mgnls) # more homogenous

R: IRFs in a SVAR model, can't display specified model

I am doing a SVAR (structural vector auto regression) analysis in which I want to plot IRFs (impulse response functions). My time series have length 137 and I only use 3 variables, furthermore I select 1 lag when specifying the VAR model.
Specifying the VAR model works fine, but when I want to summarize it I get the following error message
VAR_reduced <- VAR(VAR_data_1, p = 1, type = "both")
summary(VAR_reduced)
Error in solve.default(Sigma) :
system is computationally singular: reciprocal condition number = 1.03353e-16
From what I read in another question this error usually come up when there are not enough observations leading to overfitting, but in my example this should not be a problem, as I have enough observations.
As R does not display an error message if I don't run the summary command it is still possible to calculate the IRFs using:
plot(irf(VAR_reduced, n.ahead = 40))
But, the plot seems rather counter-intuitive, as there is no reaction from any variable other than assets. Therefore, my guess is that the error message hints at something I got wrong but haven't realised yet.
Is this correct, that is do I need to solve that error, or do my IRFs have nothing to do with this?
For completeness here is all the code:
library(quantmod)
library(urca)
library(vars)
library(tseries)
getSymbols('CPILFESL',src='FRED')
getSymbols('INDPRO',src='FRED')
getSymbols('WALCL',src='FRED')
CPI <- ts(CPILFESL, frequency = 12, start = c(1957,1))
output <- ts(INDPRO, frequency = 12, start = c(1919,1))
assets <- as.xts(WALCL)
assets <- to.monthly(assets, indexAt='yearmon', drop.time = TRUE)
assets <- ts(assets[,4], frequency = 12, start = c(2002,12))
assets <- window(assets, start = c(2008,9), end = c(2020,1))
CPI <- window(CPI, start = c(2008,9), end = c(2020,1))
output <- window(output, start = c(2008,9), end = c(2020,1))
loutput <- log(output)
lCPI <- log(CPI)
data_0 <- cbind(loutput, lCPI, assets)
plot(data_0)
VAR_data_1 <- ts.intersect(diff(loutput), diff(lCPI), diff(assets, differences = 2))
VAR_reduced <- VAR(VAR_data_1, p = 1, type = "both")
summary(VAR_reduced)

"promise already under evaluation" error in R caret's rfe function

I have a matrix X and vector Y which I use as arguments into the rfe function from the caret package. It's as simple as:
I get a weird error which I can't decipher:
promise already under evaluation: recursive default argument reference or earlier problems?
EDIT:
Here is a reproducible example for the first 5 rows of my data:
library(caret)
X_values = c(29.04,96.57,4.57,94.23,66.81,26.71,69.01,77.06,49.52,97.59,47.57,64.07,24.25,11.27,77.30,90.99,44.05,30.96,96.32,16.04)
X = matrix(X_values, nrow = 5, ncol=4)
Y = c(5608.11,2916.61,5093.05,3949.35,2482.52)
rfe(X, Y)
My R version is 3.2.3. Caret package is 6.0-76.
Does anybody know what this is?
There are two problems with your code.
You need to specify the function/algorithm that you want to fit. (this is what causes the error message you get. I am unsure why rfe throws such a cryptic error message; it makes it difficult to debug, indeed.)
You need to name your columns in the input data.
The following works:
library(caret)
X_values = c(29.04,96.57,4.57,94.23,66.81,26.71,69.01,77.06,49.52,97.59,47.57,64.07,24.25,11.27,77.30,90.99,44.05,30.96,96.32,16.04)
X = matrix(X_values, nrow = 5, ncol=4)
Y = c(5608.11,2916.61,5093.05,3949.35,2482.52)
ctrl <- rfeControl(functions = lmFuncs)
colnames(X) <- letters[1:ncol(X)]
set.seed(123)
rfe(X, Y, rfeControl = ctrl)
I chose a linear model for the rfe.
The reason for the warning messages is the low number of observations in your data during cross validation. You probably also want to set the sizes argument to get a meaningful feature elimination.

R segmented package "variable lengths differ"

I'm having trouble getting started with the segmented package in R.
When running the basic example below I get the error:
Error in model.frame.default(formula = y ~ x + U1.x + psi1.x, data = mfExt, :
variable lengths differ (found for 'x')
I was expecting segmented to return a piecewise linear model with 2 segments. I'm clearly making a mistake in my call, but am unable to work out from the error message and the documentation what my mistake is. Help would be appreciated.
require(segmented)
test.df = data.frame(x = c(1:100),
y = c(c(1:50),seq(from = 52, by = 2, length = 50)))
test.mod = lm(y ~ x,
test.df)
segmented(test.mod,
seg.Z = ~ x,
psi = list(x = 40))
It turns out that I had an object in my workspace called 'x'. After removing this object the call to segmented gave expected results.
I can replicate the error any time I have an object of length 1 called x regardless of whether that object is a list or a vector.
If the object has length greater than 1, the error disappears and segmented behaves as expected.
Weird. Thanks #Pascal for your input.

Resources