Holt Winters Model Failing with Error "Optimization Failure" in R - r

I was trying to forecast a model using holt winters in R and this failed as showing the below mentioned error. I am not sure what is the issue and how to bypass it? I am actually forecasting this on loop so, because of this error my other time series forecasts with other SKU's are not getting completed.
Can someone help me out with this. Please
Attached is the dataset which is used and the code which I ran for the same.
library(dplyr)
library(tidyverse)
library(tidyr)
library(tidymodels)
library(forecast)
library(prophet)
library(readxl)
pd1 <- readxl::read_excel("C:X/X/X")
colnames(pd1)[1]="X1"
Y1 = ts(data = pd1$X1,
frequency = 12,
start = c(2019,1),
end = c(2022,8))
training_Y1 = window(x=Y1,
end = c(2022,8))
Model_Y1 = HoltWinters(x=training_Y1,
seasonal = "additive")
The data frame pd1 is given below:
structure(list(X1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 38, 7, 7,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -44L), class = "data.frame")

Related

Adding industry dummies to 2SLS in R

for my Master Thesis I want to regress the ESG score on the stock price drop during the pandemic. For OLS this works fine. To check for potential Endogeneity I also conduct a 2SLS regression with the industry average ESG score as instrument. This works fine as long as I leave out the industry dummies. When adding them I get the following error Message:
Warning message:
In pf(w, df[1L], df[2L], lower.tail = FALSE) : NaNs produced
Moreover, the diagnostics for weak instruments and Wu-Hausman also show NaN.
I am aware of the dummy variable trap so not all industries were included.
Does anyone know why I get this error message? Any help is appreciated. Below I will provide my results with and without dummies.
Without dummies
With dummies
I managed to replicate the warning message with the first 10 rows of my data:
structure(list(NAME = c("A-MARK PRECIOUS METALS", "AAON", "AAR", "ABBOTT LABORATORIES", "ABBVIE", "ABEONA THERAPEUTICS", "ABERCROMBIE & FITCH A", "ABIOMED", "ABM INDS.", "ABRAXAS PETROLEUM"), ESG = c(30.93, 46.31, 24.66, 70.67, 79.79, 36.58, 69.13, 25.88, 72.66, 18.88 ), LogAssets = c(13.538484837701, 13.0147959839376, 14.5473975668402, 18.0628622893116, 18.8299054426017, 11.9263455151269, 15.0139386185708, 13.9751825387466, 15.1444141253049, 11.9688365085588), Quick = c(0.38, 2.27, 1.69, 1.14, 0.6, 2.26, 1.24, 4, 1.32, 0.1), I_Agri = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), I_Cons = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), I_Fin = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), I_Man = c(0, 1, 0, 1, 1, 1, 0, 1, 0, 0), I_Mining = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1), I_Serv = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0), I_Trade = c(1, 0, 0, 0, 0, 0, 1, 0, 0, 0), I_Utility = c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0), Drop = c(0.107419712070875, 0.277738886944347, 0.791427308882015, 0.297000895255148, 0.31485022307202, 0.75, 0.527002967359051, 0.222692078618225, 0.473209685729006, 0.683189655172414), Leverage = c(0.8177, 0.0178, 0.3993, 0.3623, 0.8679, 0.0169, 0.2659, 0, 0.3257, 1.5458 ), ROA = c(0.0622, 0.1926, 0.0065, 0.0726, 0.0542, -0.4324, -0.0259, 0.1888, 0.0095, -0.6467), ESG_A = c(41.9334803921569, 41.6947268673356, 42.0122772277228, 41.6947268673356, 41.6947268673356, 41.6947268673356, 41.9334803921569, 41.6947268673356, 37.5789174311926, 34.9968604651163 )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame" ))
The code I used:
library(AER)
IV2=ivreg(Drop~ESG+LogAssets+Leverage+ROA+Quick+I_Cons+I_Fin+I_Man+I_Mining+I_Serv+I_Trade+I_Utility | ESG_A+LogAssets+Leverage+ROA+Quick+I_Cons+I_Fin+I_Man+I_Mining+I_Serv+I_Trade+I_Utility, data=mwe)
summary(IV2, diagnostics = TRUE)
Rstudio Version: 2022.2.0.443 Operating system: Windows 10 pro 64 bit
Thank you!

ICC unused argument

I need to calculate the 95% CI of iCC.
I'm using this code:
icc(mydata[,c(1,2)], model = "twoway",type = "agreement", unit = "average")
I obtain this error message.
Error in icc(mydata[, c(1, 1)], model = "twoway", type = "agreement", :
unused arguments (model = "twoway", type = "agreement", unit = "average")
My data:
mydata= data.frame(A=c(0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 3), B=c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 2))
How can I fix it?
Thank you!
You might have competing packages loaded that both use icc(). The psych package and the irr package both have this as a function name. If both are loaded, the psych package is likely masking the irr command. Those are not valid arguments for psych::icc() but they are for irr::icc()
Try:
irr:: icc(mydata[,c(1,2)], model = "twoway",type = "agreement", unit = "average")

Multidimensional Scale in R - data

I would like to have a multidimensional scaling plot according to the following table (this is just a shorter form of the whole table).
I have been trying to do it in R (am quite new here...) but now. I am not even sure about that this type of data is good for multidimensional scaling. The whole table should mirror a semantic (linguistic) map (Thats why I thought that MDS should be good) and the rows mean that informants saw some pictures and gave different expressions (columns) for the pictures, so they described them differently.
The numbers in the columns are no judgments in the sense that they are on a scale from 1 to 10 or something like that but they show how many people used the expression for pic1, pic2, and so forth.
Could anyone help me to explain that MDS is actually the appropriate model I am trying to use? (Sorry, I am just too much confused after reading a lot in the last days about different methods...)
If so, here is the coding I used (just to be sure).
Thanks a lot for any advice!
daten <- structure(list(photos = c("p1", "p5", "p8", "p13", "p19", "p23", "p29", "p34", "p36", "p40", "p59", "p2", "p14"), expression1 = c(18, 8, 11, 15, 14, 16, 10, 12, 15, 18, 18, 0, 0), expression2 = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0), expression3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1), expression4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 17), expression5 = c(0, 3, 5, 0, 0, 0, 1, 5, 1, 0, 0, 0, 0), expression6 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), expression7 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), expression8 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -13L), class = c("tbl_df", "tbl", "data.frame"))
library("tibble")
has_rownames(daten)
cr<-column_to_rownames(daten, var="photo")
has_rownames(cr)
matr_cr <- as.matrix(cr[,-1])
matr_cr
d<-dist(matr_cr)
fit <- cmdscale(d, eig = TRUE, k = 2)
x <- fit$points[, 1]
y <- fit$points[, 2]
plot(x, y, xlab="Coordinate 1", ylab="Coordinate 2",
main="Multidimensional Scaling", type="n")
text(x, y, labels = row.names(matr_cr), cex=.6, col="red")
cr
Plotting multidimensional data is difficult and depending on the type of data and analysis is what to do. First of all, if you have several variables, it may be useful to cluster your data, one possible method is k-means that you can find it in the package "ClusterR". Another possible thing to do is to transform your variable by rotating the axis in order to lower the dimension with a Principal Component Analysis (PCA), you can find more about PCA in R in http://www.sthda.com/english/articles/31-principal-component-methods-in-r-practical-guide/
If you opp to plot your data as it is without a previous analysis, you may use ggplot2 package to make more useful and elegant plots. And to plot your different data attributes you can try changing size, color, shape, etc scales representing different dimensions. The problem with this option is that you can not plot several dimensions.
If I understand you well, you got pictures and people (informants) that make a review of the pictures. And the critics are separated in different levels (dimensions). If it is like that, you got as dimensions pictures, reviewers, and each level of the reviews, that make 2+N variables. Note that you can easily plot up to 5 dimensions in this kind of data, by setting x-axis and y-axis you got 2 dimensions, then you can use size scale for another dimension, color scale for another dimension, and the depending on your data and your preference you can use text or shape scale for the fifth dimension. I do not see in the table you provide the informants (reviewers) dimension. Further below you will found two examples of these plot using ggplot2, note that for shape scale a discrete variable must be used. In order to get beautiful plots and with meaning, you will have to try wich type of scale is better for each of your variables and will strongly depend on your data. Lastly, if you have several dimensions normally you should try first to assess if your data is clusterized or do a PCA.
library(ggplot2)
daten <- structure(list(photos = c("p1", "p5", "p8", "p13", "p19", "p23", "p29", "p34", "p36", "p40", "p59", "p2", "p14"), expression1 = c(18, 8, 11, 15, 14, 16, 10, 12, 15, 18, 18, 0, 0), expression2 = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0), expression3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1), expression4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 17), expression5 = c(0, 3, 5, 0, 0, 0, 1, 5, 1, 0, 0, 0, 0), expression6 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), expression7 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), expression8 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -13L), class = c("tbl_df", "tbl", "data.frame"))
# with shape scale
ggplot(data = daten,aes(x=photos, y=expression1, col=expression2, size=expression3, shape=as.factor(expression4))) +
geom_point()
# with text scale
ggplot(data = daten,aes(x=expression4, y=expression1, col=expression2, size=expression3, label=photos)) +
geom_text()

What does this error mean "order(vertex_attr(g, measure), decreasing = TRUE) : argument 1 is not a vector" in R?

I am trying to calculate robustness, a graph theory measure using R (braingraph package).
Robustness = robustness(my_networkgraph, type = c("vertex"), measure = ("btwn.cent"))
I get the following error, when I use the above robustness function:
Error in order(vertex_attr(g, measure), decreasing = TRUE) : argument 1 is not a vector
Any idea, what I am doing wrong here?
My network, which is a matrix has been converted to igraph object and robustness was calculated.
My network as a matrix:
mynetwork <- matrix(c(0, 1, 0, 1, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 1, 0, 1, 1,
0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0), nrow = 8)
This matrix was converted as igraph using the following code:
my_networkgraph <-graph_from_adjacency_matrix(mynetwork, mode = c("undirected"),weighted = NULL, diag = TRUE, add.colnames = NULL, add.rownames = NA)
Please help me to understand the above error
Thanks
Priya
There was a bug in the above function. To run the robustness code, you will need to supply a vertex attribute to your network: V(network)$degree <- degree(network) V(network)$btwn.cent <- centr_betw(network)$res

Planned Contrasts on glmmTMB

Apologies if this is a repeat question. Many have posted looking looking for a way to do post-hoc analyses on the conditional model (fixed factors) in glmmTMB. I want to do plannned contrasts between certain groups, not test every pairwise comparison (e.g. Tukey).
The code below worked well on nlme:lme for a lmm. However, it returns an error on the code below.
Error in modelparm.default(model, ...) :
dimensions of coefficients and covariance matrix don't match
Is there a way to do planned contrasts on a glmmTMB?
#filtdens is a dataframe and TRT,DATE,BURN,VEG are factors
filtdens <- merged %>% filter(!BLOCK %in% c("JB2","JB4","JB5") & MEAS =="DENS" &
group == "TOT" & BURN == "N" & VEG == "C")
filtdens$TD <- interaction(filtdens$TRT, filtdens$DATE)
mod2 <- glmmTMB(count~(TD)+(1|BLOCK),
data=filtdens,
zi=~1,
family=nbinom1(link = "log"))
k1 <- matrix(c(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1), byrow = T, ncol = 12)
summary(glht(mod2, linfct=k1),test=adjusted("bonferroni"))
A reproducible example would be helpful, but: this vignette in the development version offers code that ought to enable multcomp::linfct, i.e.:
glht_glmmTMB <- function (model, ..., component="cond") {
glht(model, ...,
coef. = function(x) fixef(x)[[component]],
vcov. = function(x) vcov(x)[[component]],
df = NULL)
}
modelparm.glmmTMB <- function (model,
coef. = function(x) fixef(x)[[component]],
vcov. = function(x) vcov(x)[[component]],
df = NULL, component="cond", ...) {
multcomp:::modelparm.default(model, coef. = coef., vcov. = vcov.,
df = df, ...)
}
Test (this example is with Tukey, but I don't see why it shouldn't work more generally ...)
library(glmmTMB)
data("cbpp",package="lme4")
cbpp_b1 <- glmmTMB(incidence/size~period+(1|herd),
weights=size,family=binomial,
data=cbpp)
g1 <- glht(cbpp_b1, linfct = mcp(period = "Tukey"))
summary(g1)
This works with the current CRAN version, but the current development version of glmmTMB offers more options (e.g. emmeans(); see the above-linked vignette). You'll need to install via devtools::install_github("glmmTMB/glmmTMB/glmmTMB") (you'll need compilation tools installed as well).

Resources