in R, how to capture ARIMA elements from auto.arima results - r

I have a bunch of series to forecast using forecast::auto.arima function. I like to save what type of model did auto.arima fit. If you run the following code:
library(forecast)
set.seed(123)
y <- sin(seq(-pi,pi,0.05))+(rnorm(length(seq(-pi,pi,0.05)))/4)
arima.model <- auto.arima(y)
arima.model
the result of the last line execution shows
Series: y
**ARIMA(1,1,2)**
Coefficients:
ar1 ma1 ma2
0.9594 -1.7285 0.7740
s.e. 0.0380 0.0745 0.0658
sigma^2 estimated as 0.06534: log likelihood=-6.1
AIC=20.2 AICc=20.53 BIC=31.51
How can I capture ARIMA(1,1,2) and save results? I was hoping to do something like arima.model$ and capture what I need to but I could not figure it out.

You can try summary(arima.model), arima.model$coef, arima.model$aic, arima.model$bic.
If you want a tidy format, you can use broom package like this:
library(broom)
tidy(arima.model) #ar/ma terms
glance(arima.model) #information criteria
tidy(arima.model)
# A tibble: 3 x 3
term estimate std.error
<fct> <dbl> <dbl>
1 ar1 0.959 0.0380
2 ma1 -1.73 0.0745
3 ma2 0.774 0.0658
glance(arima.model)
# A tibble: 1 x 4
sigma logLik AIC BIC
<dbl> <dbl> <dbl> <dbl>
1 0.256 -6.10 20.2 31.5

Related

Creating data frame with CIs from matching and poisson model

After doing ps matching, I'm running a poisson model like so:
model <- glm(outcome ~ x1 + x2 + x3 ... ,
data = d,
weights = psweights$weights,
family = "poisson")
And then want to create a new data frame with the variable names, coefficients, and upper and lower confidence intervals. Just doing:
d2 <- summary(model)$coef
gets me the variable names, coefficients, standard errors, and z values. What is the easiest way to compute confidence intervals, convert them into columns and bind it all into one data frame?
How about this, using the broom package:
library(broom)
mod <- glm(hp ~ disp + drat + cyl, data=mtcars, family=poisson)
tidy(mod, conf.int=TRUE)
#> # A tibble: 4 × 7
#> term estimate std.error statistic p.value conf.low conf.high
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 (Intercept) 2.40 0.196 12.3 1.30e-34 2.02 2.79
#> 2 disp 0.000766 0.000259 2.96 3.07e- 3 0.000258 0.00127
#> 3 drat 0.240 0.0386 6.22 4.89e-10 0.164 0.315
#> 4 cyl 0.236 0.0195 12.1 1.21e-33 0.198 0.274
Created on 2022-06-30 by the reprex package (v2.0.1)

How to get the summary from lm() in a table?

I have trained lm model on a dataset and generated the summary of the model using summary() function. How to get the summary in a table?
You can use broom::tidy :
model <- lm(mpg~cyl, mtcars)
broom::tidy(model)
# term estimate std.error statistic p.value
# <chr> <dbl> <dbl> <dbl> <dbl>
#1 (Intercept) 37.9 2.07 18.3 8.37e-18
#2 cyl -2.88 0.322 -8.92 6.11e-10

How to export mlogit results to excel?

Does exist any package which can help me to export results of multinomial logit to excel for example like a table?
The broom package does a reasonable job of tidying multinomial output.
library(broom)
library(nnet)
fit.gear <- multinom(gear ~ mpg + factor(am), data = mtcars)
summary(fit.gear)
Call:
multinom(formula = gear ~ mpg + factor(am), data = mtcars)
Coefficients:
(Intercept) mpg factor(am)1
4 -11.15154 0.5249369 11.90045
5 -18.39374 0.3662580 22.44211
Std. Errors:
(Intercept) mpg factor(am)1
4 5.317047 0.2680456 66.895845
5 67.931319 0.2924021 2.169944
Residual Deviance: 28.03075
AIC: 40.03075
tidy(fit.gear)
# A tibble: 6 x 6
y.level term estimate std.error statistic p.value
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 4 (Intercept) 1.44e-5 5.32 -2.10 3.60e- 2
2 4 mpg 1.69e+0 0.268 1.96 5.02e- 2
3 4 factor(am)1 1.47e+5 66.9 0.178 8.59e- 1
4 5 (Intercept) 1.03e-8 67.9 -0.271 7.87e- 1
5 5 mpg 1.44e+0 0.292 1.25 2.10e- 1
6 5 factor(am)1 5.58e+9 2.17 10.3 4.54e-25
Then use the openxlsx package to send that to Excel.
library(openxlsx)
write.xlsx(file="E:/.../fitgear.xlsx", tidy(fit.gear))
(Note that the tidy function exponentiates the coefficients by default, although the help page incorrectly says the default is FALSE). So these are relative risk ratios, which is why they don't match the output of summary. And if you want confidence intervals, you have to ask for them.)

How to extract scaled residuals from glmer summary

I am trying to write a .csv file that appends the important information from the summary of a glmer analysis (from the package lme4).
I have been able to isolate the coefficients, AIC, and random effects , but I have not been able to isolate the scaled residuals (Min, 1Q, Median, 3Q, Max).
I have tried using $residuals, but I get a very long output, not the information shown in the summary.
> library(lme4)
> setwd("C:/Users/Arthur Scully/Dropbox/! ! ! ! PHD/Chapter 2 Lynx Bobcat BC/ResourceSelection")
> #simple vectors
>
> x <- c("a","b","b","b","b","d","b","c","c","a")
>
> y <- c(1,1,0,1,0,1,1,1,1,0)
>
>
> # Simple data frame
>
> aes.samp <- data.frame(x,y)
> aes.samp
x y
1 a 1
2 b 1
3 b 0
4 b 1
5 b 0
6 d 1
7 b 1
8 c 1
9 c 1
10 a 0
>
> # Simple glmer
>
> aes.glmer <- glmer(y~(1|x),aes.samp,family ="binomial")
boundary (singular) fit: see ?isSingular
>
> summary(aes.glmer)
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
Family: binomial ( logit )
Formula: y ~ (1 | x)
Data: aes.samp
AIC BIC logLik deviance df.resid
16.2 16.8 -6.1 12.2 8
I can isolate information above by using the call summary(aes.glmer)$AIC
Scaled residuals:
Min 1Q Median 3Q Max
-1.5275 -0.9820 0.6546 0.6546 0.6546
I do not know the call to isolate the above information
Random effects:
Groups Name Variance Std.Dev.
x (Intercept) 0 0
Number of obs: 10, groups: x, 4
I can isolate this information using the ranef function
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.8473 0.6901 1.228 0.22
And I can isolate the information above using summary(aes.glmer)$coefficient
convergence code: 0
boundary (singular) fit: see ?isSingular
>
> #Pull important
> ##write call to select important output
> aes.glmer.coef <- summary(aes.glmer)$coefficient
> aes.glmer.AIC <- summary(aes.glmer)$AIC
> aes.glmer.ran <-ranef(aes.glmer)
>
> ##
> data.frame(c(aes.glmer.coef, aes.glmer.AIC, aes.glmer.ran))
X0.847297859077025 X0.690065555425105 X1.22785125618255 X0.219502810378876 AIC BIC logLik deviance df.resid X.Intercept.
a 0.8472979 0.6900656 1.227851 0.2195028 16.21729 16.82246 -6.108643 12.21729 8 0
b 0.8472979 0.6900656 1.227851 0.2195028 16.21729 16.82246 -6.108643 12.21729 8 0
c 0.8472979 0.6900656 1.227851 0.2195028 16.21729 16.82246 -6.108643 12.21729 8 0
d 0.8472979 0.6900656 1.227851 0.2195028 16.21729 16.82246 -6.108643 12.21729 8 0
If anyone knows what call I can use to isolate the "scaled residuals" I would be very greatful.
I haven't got your data, so we'll use example data from the lme4 vignette.
library(lme4)
library(lattice)
library(broom)
gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
data = cbpp, family = binomial)
This is for the residuals. tidy from the broom package puts it in to a tibble, which you can then export to a csv.
x <- tidy(quantile(residuals(gm1, "pearson", scaled = TRUE)))
x
# A tibble: 5 x 2
names x
<chr> <dbl>
1 0% -2.38
2 25% -0.789
3 50% -0.203
4 75% 0.514
5 100% 2.88
Also here are some of the other bits that you might find useful, using glance from broom.
y <- glance(gm1)
y
# A tibble: 1 x 6
sigma logLik AIC BIC deviance df.residual
<dbl> <dbl> <dbl> <dbl> <dbl> <int>
1 1 -92.0 194. 204. 73.5 51
And
z <- tidy(gm1)
z
# A tibble: 5 x 6
term estimate std.error statistic p.value group
<chr> <dbl> <dbl> <dbl> <dbl> <chr>
1 (Intercept) -1.40 0.231 -6.05 1.47e-9 fixed
2 period2 -0.992 0.303 -3.27 1.07e-3 fixed
3 period3 -1.13 0.323 -3.49 4.74e-4 fixed
4 period4 -1.58 0.422 -3.74 1.82e-4 fixed
5 sd_(Intercept).herd 0.642 NA NA NA herd

Calculating the standard errors of autoregressive terms and sigma in censored regression model

I have fitted a censored regression model in R using carx package.
The data I'm using has 130 observations
head(SAdata)
# A tibble: 6 x 10
CC lcl Dum ci CPI LI FDI FPI OI Inflows
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 0.617 2.00 0 2.00 4.94 23.3 -0.207 -0.0320 -0.924 -1.16
2 0.410 2.00 0 2.00 2.92 20.5 -0.246 -0.128 0.0749 -0.299
3 0.196 2.00 0 2.00 4.26 17.2 -0.177 -0.0763 -0.482 -0.736
4 0.0518 2.00 0 2.00 5.90 15.5 0.00493 -0.313 -1.59 -1.90
5 -0.255 2.00 0 2.00 3.29 14.8 -0.0187 -0.200 -1.38 -1.59
6 -0.392 2.00 0 2.00 4.43 14 -0.00292 -0.0860 -0.00519 -0.0941
> dim(SAdata)
[1] 130 10
library(carx)
SAmodel <- carx(y=SAdata$CC, x=SAdata[,c("CPI","LI","FDI","FPI", "OI")], lcl=1.996866, p=2, CI.compute = FALSE, CI.level = 0.95)
summary(SAmodel)
Call:
carx.default(y = SAdata$CC, x = SAdata[, c("CPI", "LI", "FDI", "FPI", "OI")], lcl = 1.996866, p = 2, CI.compute = FALSE, CI.level = 0.95)
Coefficients:
Estimate
CPI 0.0804
LI -0.0088
FDI -0.0395
FPI -0.0166
OI 0.0488
AR1 1.6324
AR2 -0.7097
sigma 0.4286
AIC:
[1] -72.92045
But what I got in return is only the estimates without confidence intervals, the confidence intervals in this package are calculated with bootstrapping, which takes a lot of time, when I set the argument IC.compute = T with 1000 iterations (from what've read that is the least number of iterations), it took a whole day and it didn't finish. For this reason, I tried to manually calculate the standard errors of every coefficient, but I couldn't find a way to calculate the standard error for the autoregressive terms and sigma.
Here's what I tried
#Calculating the residuals of the fitted model
SAres = residuals(SAmodel,type="raw")
# Find the sum of the squared residuals
rss <- sum(SAres^2)
# And use that to find the estimate of sigma^2, commonly called S
S <- sqrt(rss / (length(SAres) - length(SAmodel$coefficients)))
# Make the X matrix; a column of 1s for the intercept and one for each variable
X <- cbind(rep( nrow(SAdata)), SAdata$CPI, SAdata$LI,SAdata$FDI, SAdata$FPI,
SAdata$OI)
# Multiply matrices using %*%, transpose them with t(),
# and invert them with solve(); and directly apply the formula above with:
std.errors <- S * sqrt(diag(solve(t(X) %*% X)))
std.errors
[1] 0.001232184 0.037669933 0.010483153 0.068843648 0.040779940 0.063888636
I don't how to include the autoregressive terms (AR1, AR2) and sigma, because their calculations are based on the response variable. How to get standard errors of these parameters?
I need to calculate the standard errors so later I'll be able to compute the confidence intervals of each coefficient.
Any help is deeply appreciated

Resources