Get ARIMA white noise with known parameter in R - r

I have a MA(1) model with known parameter and known .
I'd like to know, is there a function in R that can return for me?
I also tried to get by iteration:
However, in reality, is unknown and cannot be specified at the first place.
I'm having this question because I used gnls to estimate a nonlinear model with residuals being MA(1) process. The code is something like:
model = gnls(y ~ c + log( x1^g + x2^g), start = list(c = 0.04, g = 0.3),
correlation = corARMA(c(0.5), form = ~ 1, p = 0, q = 1, fixed = FALSE))
It returns every parameter estimation including . But residuals(model) returns instead of .
So any suggestions?
Thank you for the help in advance.

Yes. You can use Arima function available in R.
fit <- arima(ts(data), order=c(0,0,1))
as you do not want AR and I part. You can set it to zero.
summary(fit)
You can observe parameters learned and errors by summary function.
For more information, refer to : https://www.otexts.org/fpp/8/7

Related

Parameters of truncated normal distribution using R

How can we numerically solve these equations using R when E_(μ,σ) (X)=1 and 〖var〗_(μ,σ) (X)=1 ? I am interested in finding the values of μ and σ.
Here α=(a-μ)/σ and β=(b-μ)/σ. I used the following code, but I'm not getting an answer. Is there any other code or method I may use to get what I want ?
mubar<-1
sigmabar<-1
a<-0.5
b<-5.5
model <- function(x)c(F1 = mubar-x[1]+x[2]*((pnorm((b-x[1])/x[2])-pnorm(a-x[1])/x[2])/(dnorm((b-x[1])/x[2])-dnorm((a-x[1])/x[2]))),
F2 = sigmabar^2-x[2]^2*(1-(((b-x[1])/x[2]*pnorm((b-x[1])/x[2])-(a-x[1])/x[2]*pnorm((a-x[1])/x[2]))/(dnorm((b-x[1])/x[2])-dnorm((a-x[1])/x[2])))-((pnorm((b-x[1])/x[2])-pnorm((a-x[1])/x[2]))/(dnorm((b-x[1])/x[2])-dnorm((a-x[1])/x[2])))^2) )
(ss <- multiroot(f = model, start = c(1, 1)))

Rmd knit produces different results

I am doing ridge regression model using cv.glmnet(), but the knit (to HTML) outputs are very different than console outputs. I already used set.seed() function but it doesn't work. Here's the code I wrote:
set.seed(90)
lambdas <- 10^seq(2, -3, by = -.1) # list of lambdas to find out the best one for the model
fit <- cv.glmnet(training_data_X, training_data_Y, alpha = 0, lambda = lambdas) # fit the model
lambda_optimal <- min(fit$lambda) # get the optimal lambda according to the fitted model
fit_optimal <- glmnet(training_data_X, training_data_Y, alpha = 0, lambda = lambda_optimal) # fit a model again with optimal lambda
test_data_Y$pred <- exp(predict(fit_optimal, s = lambda_optimal, newx = test_data_X))
sst_test <- sum((test_data_Y$truth_values - mean(test_data_Y$truth_values))^2)
sse_test <- sum((test_data_Y$truth_values - test_data_Y$pred)^2)
r_square_test <- 1 - sse_test / sst_test
r_square_test # R-squared value of the test set
The R-Squared value is very much different from the console output. And when I checked the test_data_Y table, I see that my predictions and truth values are also different from the console values.
How can I solve this issue?
Thank you in advance.

Using tsboot to obtain confidence interval from a regression with lags

I would like to do a bootstrap of regression coefficient in a return model that includes two lags.
I have snp_ret vector with returns obtained from quantmod. The data looks like this:
head(snp_ret)
ret
1998-10-13 -0.2920975
1998-10-14 1.0728374
1998-10-15 4.0882022
1998-10-16 0.8489058
1998-10-19 0.5635226
1998-10-20 0.1448549
Obtaining bootstrap for coefficients should be simple:
getC=function(myData){
return(coef(lm(formula = dyn(ret ~ lag(ret, c(-1,-9))), data=myData) ))
}
tsboot(snp_ret, getC, R = 100, l = 18, sim = "fixed")
The following error appears:
Error in merge.zoo(ret, lag(ret, c(-1, -9)), retclass = "list", all
= TRUE) : series cannot be merged with non-unique index entries in a series
I suspect that it has to do with the fact that regression has two lags, but do not know how to proceed.
If possible, please help.
All right, I found a workaround, so maybe this will be interesting to somebody else... Using arima function instead of lag operators helped.
getC <- function(myData) {
reg <- suppressWarnings(arima(myData, order = c(9, 0, 0), fixed = c(NA, 0,0,0,0,0,0,0,NA,NA)))
return((coef(reg)[c(1,9,10)]))
Note that arima has a weird way of selecting lags - you have to force to zero coefficients on lags that you don't want to include

Using R INLA hyperparameter to.theta and from.theta functions

R-INLA model hyperparameters have to.theta and from.theta functions that appear to be for converting between different parameterisations. It would be convenient to use those conversion functions but how does one do so?
Example with ar1
From the ar1 documentation (http://www.math.ntnu.no/inla/r-inla.org/doc/latent/ar1.pdf):
The parameter rho is represented as theta_2 = log((1 + rho)/(1 - rho))
and further down under hyper, theta2 we have to.theta 'function(x) log((1+x)/(1-x))'. It would be nice if we could use that to convert between rho and theta_2.
Let's try using an example
library(INLA)
# Example from ar1 documentation (http://www.math.ntnu.no/inla/r-inla.org/doc/latent/ar1.pdf)
#simulate data
n = 100
rho = 0.8
prec = 10
## note that the marginal precision would be
marg.prec = prec * (1-rho^2)
E=sample(c(5,4,10,12),size=n,replace=T)
eta = as.vector(arima.sim(list(order = c(1,0,0), ar = rho), n = n,sd=sqrt(1/prec)))
y=rpois(n,E*exp(eta))
data = list(y=y, z=1:n, E=E)
## fit the model
formula = y~f(z,model="ar1")
result = inla(formula,family="poisson", data = data, E=E)
That runs fine.
Can we use to.theta like this?
formula.to.theta = y~f(z,model="ar1",
hyper = list(rho = list(initial = to.theta(0.25))))
result = inla(formula.to.theta,family="poisson", data = data, E=E)
# Error in to.theta(0.25) : could not find function "to.theta"
So we can't use it like that. Is there another way to specify formula.to.theta that would work?
Pretty sure the answer to your question is "no". The Documentation is saying, not that there are functions by those names in the package, but rather that the hyper hyperparameter element will have functions by those names with values as given in the documentation. There is no reason to think that pasting those names after formula. would result in a meaningful function. Here is how to examine the value of from.theta in the environment of a specific call to the f-function:
library(INLA)
eval( f(z, model = "ar1") )$hyper$theta3$from.theta
===== result ========
function (x)
x
<environment: 0x7fdda6214040>
attr(,"inla.read.only")
[1] TRUE
The result from f( , "ar1") actually has three theta's each with a to and from function. You may be trying to change the hyper$thetax$param value which does not have an attr(,"inla.read.only") value of TRUE.
It would probably be more informative for you to execute this:
eval( f(z, model = "ar1") )$hyper

vegan::ordiR2step() doesn't find best-fit model

The vegan package includes the ordiR2step() function for model building, which can be used to identify the most important variables using the R2 and the p-value as goodness of fit measures. However for the dataset I was recently working with the function doesn't provide the best-fit model.
# data
RIKZ <- read.table("http://www.uni-koblenz-landau.de/en/campus-landau/faculty7/environmental-sciences/landscape-ecology/Teaching/RIKZ_data/at_download/file", header = TRUE)
# data preparation
Species <- RIKZ[ ,2:5]
ExplVar <- RIKZ[ , 9:15]
Species_fin <- Species[ rowSums(Species) > 0, ]
ExplVar_fin <- ExplVar[ rowSums(Species) > 0, ]
# rda
RIKZ_rda <- rda(Species_fin ~ . , data = ExplVar_fin, scale = TRUE)
# stepwise model building: ordiR2step()
require(vegan)
step_both_R2 <- ordiR2step(rda(Species_fin ~ salinity, data = ExplVar_fin, scale = TRUE),
scope = formula(RIKZ_rda),
direction = "both", R2scope = TRUE, Pin = 0.05,
steps = 1000)
Why does ordiR2step() not add the variable exposure to the model, although it would increase the explained variance?
If R2scope is set FALSE and the p-value criterion is increased (Pin = 0.15) it adds the variable exposure corretly but throws the following error:
Error in terms.formula(tmp, simplify = TRUE) :
invalid model formula in ExtractVars
If R2scope is set TRUE (Pi = 0.15) exposure is not added.
Note: This might seem more as a statistic question and therefore more suitable for CV. However I think the problem is rather technical and better off here on SO.
Please read the ordiR2step documentation: it will tell you why exposure is not added to the model. The help page tells that ordiR2step has three stopping criteria. The second criterion is that "the adjusted R2 of the ‘scope’ is exceeded". This happens with exposure and therefore it was not added. This second criterion will be ignored if you set R2scope = FALSE (also documented). So the function works like documented.

Resources