How to extract adjusted R squared in vars package? - r

This question is highly correlated with the question from this link. How to extract p-value in var package?
I just would like to take adjusted R squared from VARS package..
Even though there is a similar question, I don't have any idea to modify to take adjusted r square.. please help me.
I just followed previous example.
library(vars)
symbols=c('^N225','^FTSE','^GSPC')
getSymbols(symbols,src='yahoo', from="2003-04-28", to="2007-10-29")
period="daily"
A1=periodReturn(N225$N225.Adjusted,period=period)
B1=periodReturn(FTSE$FTSE.Adjusted,period=period)
C1=periodReturn(GSPC$GSPC.Adjusted,period=period)
datap_1<-cbind(A1,B1,C1)
datap_1<-na.omit(datap_1)
datap_1<-(datap_1)^2
vardatap_3<-VAR(datap_1,p=3,type="none")
summary(vardatap_3)
Then the summary can be presented like..
VAR Estimation Results:
=========================
Endogenous variables: N225, FTSE, SP500
Deterministic variables: none
Sample size: 1055
Log Likelihood: 23637.848
Roots of the characteristic polynomial:
0.8639 0.6224 0.6224 0.5711 0.5711 0.5471 0.5471 0.4683 0.4683
Call:
VAR(y = datap_1, p = 3, type = "none")
Estimation results for equation N225:
=====================================
N225 = N225.l1 + FTSE.l1 + SP500.l1 + N225.l2 + FTSE.l2 + SP500.l2 + N225.l3 + FTSE.l3 + SP500.l3
Estimate Std. Error t value Pr(>|t|)
N225.l1 0.03436 0.03116 1.103 0.270
FTSE.l1 0.47025 0.06633 7.089 2.48e-12 ***
SP500.l1 0.60717 0.07512 8.083 1.74e-15 ***
N225.l2 0.14938 0.03057 4.886 1.19e-06 ***
FTSE.l2 -0.05440 0.06744 -0.807 0.420
SP500.l2 -0.09024 0.07782 -1.160 0.246
N225.l3 0.16809 0.02924 5.749 1.18e-08 ***
FTSE.l3 0.04480 0.06597 0.679 0.497
SP500.l3 -0.01007 0.07941 -0.127 0.899
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.0002397 on 1046 degrees of freedom
Multiple R-Squared: 0.3099, Adjusted R-squared: 0.304
F-statistic: 52.2 on 9 and 1046 DF, p-value: < 2.2e-16

Adjusted r squared values can be accessed in output of function summary() and list element varresult. varresult contains summary tables for each of daily returns.
> lapply(summary(vardatap_3)$varresult, "[", "adj.r.squared")
$daily.returns
$daily.returns$adj.r.squared
[1] 0.3039812
$daily.returns.1
$daily.returns.1$adj.r.squared
[1] 0.3201587
$daily.returns.2
$daily.returns.2$adj.r.squared
[1] 0.1972104

Related

model selection using StepAIC; how can I see other models besides my final model?

I am conducting model selection on my dataset using the package MASS and the function stepAIC. This is the current code I am using:
mod <- lm(Distance~DiffAge + DiffR + DiffSize + DiffRep + DiffSeason +
Diff.Bkp + Diff.Fzp + Diff.AO + Diff.Aow +
Diff.Lag.NAOw + Diff.Lag.NAO + Diff.Lag.AO + Diff.Lag.Aow, data=data,
na.action="na.exclude")
library(MASS)
step.model<-stepAIC(mod, direction = "both",
trace = FALSE)
summary(step.model)
this gives me the following output:
Call:
lm(formula = Distance ~ Diff.Lag.NAOw + Diff.Lag.AO + DiffSeason,
data = data, na.action = "na.exclude")
Residuals:
Min 1Q Median 3Q Max
-146.984 -48.397 -9.533 42.169 194.950
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 77.944 20.247 3.850 0.000184 ***
Diff.Lag.NAOw 11.868 6.261 1.896 0.060209 .
Diff.Lag.AO 24.696 17.475 1.413 0.159947
DiffSeasonEW-LW 41.891 18.607 2.251 0.026014 *
DiffSeasonLW-LW 22.863 20.791 1.100 0.273465
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 67.2 on 132 degrees of freedom
Multiple R-squared: 0.06031, Adjusted R-squared: 0.03183
F-statistic: 2.118 on 4 and 132 DF, p-value: 0.08209
If I am reading this right, the output only shows me the top model (Let me know if this is incorrect!). I would like to see the other, lower-ranked models as well, with their accompanying AIC scores.
Any suggestions on how I can achieve this? Should I modify my code in any way?

Marginal Effects of conditional logit model in R using, "clogit," function

I am trying to figure out how to calculate the marginal effects of my model using the, "clogit," function in the survival package. The margins package does not seem to work with this type of model, but does work with "multinom" and "mclogit." However, I am investigating the affects of choice characteristics, and not individual characteristics, so it needs to be a conditional logit model. The mclogit function works with the margins package, but these results are widely different from the results using the clogit function, why is that? Any help calculating the marginal effects from the clogit function would be greatly appreciated.
mclogit output:
Call:
mclogit(formula = cbind(selected, caseID) ~ SysTEM + OWN + cost +
ENVIRON + NEIGH + save, data = atl)
Estimate Std. Error z value Pr(>|z|)
SysTEM 0.139965 0.025758 5.434 5.51e-08 ***
OWN 0.008931 0.026375 0.339 0.735
cost -0.103012 0.004215 -24.439 < 2e-16 ***
ENVIRON 0.675341 0.037104 18.201 < 2e-16 ***
NEIGH 0.419054 0.031958 13.112 < 2e-16 ***
save 0.532825 0.023399 22.771 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Null Deviance: 18380
Residual Deviance: 16670
Number of Fisher Scoring iterations: 4
Number of observations: 8364
clogit output:
Call:
coxph(formula = Surv(rep(1, 25092L), selected) ~ SysTEM + OWN +
cost + ENVIRON + NEIGH + save + strata(caseID), data = atl,
method = "exact")
n= 25092, number of events= 8364
coef exp(coef) se(coef) z Pr(>|z|)
SysTEM 0.133184 1.142461 0.034165 3.898 9.69e-05 ***
OWN -0.015884 0.984241 0.036346 -0.437 0.662
cost -0.179833 0.835410 0.005543 -32.442 < 2e-16 ***
ENVIRON 1.186329 3.275036 0.049558 23.938 < 2e-16 ***
NEIGH 0.658657 1.932195 0.042063 15.659 < 2e-16 ***
save 0.970051 2.638079 0.031352 30.941 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
exp(coef) exp(-coef) lower .95 upper .95
SysTEM 1.1425 0.8753 1.0685 1.2216
OWN 0.9842 1.0160 0.9166 1.0569
cost 0.8354 1.1970 0.8264 0.8445
ENVIRON 3.2750 0.3053 2.9719 3.6091
NEIGH 1.9322 0.5175 1.7793 2.0982
save 2.6381 0.3791 2.4809 2.8053
Concordance= 0.701 (se = 0.004 )
Rsquare= 0.103 (max possible= 0.688 )
Likelihood ratio test= 2740 on 6 df, p=<2e-16
Wald test = 2465 on 6 df, p=<2e-16
Score (logrank) test = 2784 on 6 df, p=<2e-16
margins output for mclogit
margins(model2A)
SysTEM OWN cost ENVIRON NEIGH save
0.001944 0.000124 -0.001431 0.00938 0.00582 0.0074
margins output for clogit
margins(model2A)
Error in match.arg(type) :
'arg' should be one of “risk”, “expected”, “lp”

Different results of lm with same dataset written in two different languages (English and Korean)

Results of lm function applied on two dataset (numeric variables + categorical variables) written in two different languages (one written in English and the other one written in Korean) are different. Except the categorical variables, numeric variable are exactly the same. What could explain the difference in the results?
#data
df3 <- repmis::source_DropboxData("df3_v0.1.csv","gg30a74n4ew3zzg",header = TRUE)
#the one written in korean
out1<-lm(YD~SANJI+TAmin8+TMINup18do6+typ_rain6+DTD9,data=df3)
summary(out1)
#the one written in eng
df3$SANJI[df3$SANJI=="전북"]<-"JB"
df3$SANJI[df3$SANJI=="충북"]<-"CHB"
df3$SANJI[df3$SANJI=="경북"]<-"KB"
df3$SANJI[df3$SANJI=="전남"]<-"JN"
df3$SANJI2[df3$SANJI2=="고창"]<-"Gochang"
df3$SANJI2[df3$SANJI2=="괴산"]<-"Goesan"
df3$SANJI2[df3$SANJI2=="단양"]<-"Danyang"
df3$SANJI2[df3$SANJI2=="봉화"]<-"Fenghua"
df3$SANJI2[df3$SANJI2=="신안"]<-"Sinan"
df3$SANJI2[df3$SANJI2=="안동"]<-"Andong"
df3$SANJI2[df3$SANJI2=="영광"]<-"younggang"
df3$SANJI2[df3$SANJI2=="영양"]<-"youngyang"
df3$SANJI2[df3$SANJI2=="영주"]<-"youngju"
df3$SANJI2[df3$SANJI2=="예천"]<-"Yecheon"
df3$SANJI2[df3$SANJI2=="의성"]<-"Yusaeng"
df3$SANJI2[df3$SANJI2=="제천"]<-"Jechon"
df3$SANJI2[df3$SANJI2=="진안"]<-"Jinan"
df3$SANJI2[df3$SANJI2=="청송"]<-"Changsong"
df3$SANJI2[df3$SANJI2=="해남"]<-"Haenam"
out2<-lm(YD~SANJI+TAmin8+TMINup18do6+typ_rain6+DTD9,data=df3)
summary(out2)
#the one written in korean
#Call:
#lm(formula = YD ~ SANJI + TAmin8 + TMINup18do6 + typ_rain6 +
# DTD9, data = df3)
#Residuals:
# Min 1Q Median 3Q Max
#-98.836 -23.173 -2.261 22.626 111.367
#Coefficients:
# Estimate Std. Error t value Pr(>|t|)
#(Intercept) 970.33251 84.12479 11.534 < 2e-16 ***
#SANJI전남 -33.75664 12.53277 -2.693 0.008158 **
#SANJI전북 -44.17939 11.22274 -3.937 0.000144 ***
#SANJI충북 -44.09285 9.16736 -4.810 4.74e-06 ***
#TAmin8 -25.56618 3.36053 -7.608 9.37e-12 ***
#TMINup18do6 4.58052 0.96528 4.745 6.19e-06 ***
#typ_rain6 -0.19754 0.02862 -6.903 3.23e-10 ***
#DTD9 -16.15975 2.65128 -6.095 1.59e-08 ***
#---
#Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#Residual standard error: 37.2 on 112 degrees of freedom
#Multiple R-squared: 0.58, Adjusted R-squared: 0.5538
#F-statistic: 22.1 on 7 and 112 DF, p-value: < 2.2e-16
#the one written in eng
#Call:
#lm(formula = YD ~ SANJI + TAmin8 + TMINup18do6 + typ_rain6 +
# DTD9, data = df3)
#Residuals:
# Min 1Q Median 3Q Max
#-98.836 -23.173 -2.261 22.626 111.367
#Coefficients:
# Estimate Std. Error t value Pr(>|t|)
#(Intercept) 926.23966 84.32621 10.984 < 2e-16 ***
#SANJIJB -0.08654 12.32752 -0.007 0.994
#SANJIJN 10.33620 13.09434 0.789 0.432
#SANJIKB 44.09285 9.16736 4.810 4.74e-06 ***
#TAmin8 -25.56618 3.36053 -7.608 9.37e-12 ***
#TMINup18do6 4.58052 0.96528 4.745 6.19e-06 ***
#typ_rain6 -0.19754 0.02862 -6.903 3.23e-10 ***
#DTD9 -16.15975 2.65128 -6.095 1.59e-08 ***
#---
#Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#Residual standard error: 37.2 on 112 degrees of freedom
#Multiple R-squared: 0.58, Adjusted R-squared: 0.5538
#F-statistic: 22.1 on 7 and 112 DF, p-value: < 2.2e-16
Your overall model fits are the same, you just have different reference classes for your factor ("SANJIJ"). Having a different reference level will also affect your intercept but won't change the estimation of your continuous covariates.
You can use relevel() to force a particular reference class (assuming SANJIJ is already a factor) or explicitly create the factor() with a levels= parameter, otherwise the default order is sorted alphabetically and the levels may not sort the same way in the different languages.

How to memorize one element from summary(VAR((b, p = 3, type = "const")) in R?

After running summary(VAR((b, p = 3, type = "const")), I get the result shown at the end of this question.
I would like to memorize not all results, but only one value: diff_gov.l3 **0.37446** (Estimate).
I suppose that my code should start with summary(VAR((b, p = 3, type = "const"))$SOMETHING, but I do not know how to define the position of diff_gov.l3 in column Estimate (0.37446).
Do you have any suggestion how can I define the position of the value I need (summary(VAR((b, p = 3, type = "const"))$SOMETHING) or is there a way to see the position of every element in the result of summary() code?
RESULT:
summary(VAR(b, p = 3, type = "const"))
VAR Estimation Results:
=========================
Endogenous variables: diff_gov, diff_hh
Deterministic variables: const
Sample size: 47
Log Likelihood: 64.057
Roots of the characteristic polynomial:
0.7848 0.7848 0.7722 0.693 0.693 0.5438
Call:
VAR(y = b, p = 3, type = "const")
Estimation results for equation diff_gov:
=========================================
diff_gov = diff_gov.l1 + diff_hh.l1 + diff_gov.l2 + diff_hh.l2 + diff_gov.l3 + diff_hh.l3 + const
Estimate Std. Error t value Pr(>|t|)
diff_gov.l1 0.18760 0.15514 1.209 0.23366
diff_hh.l1 0.06080 0.04760 1.277 0.20889
diff_gov.l2 -0.35682 0.19484 -1.831 0.07450 .
diff_hh.l2 0.14308 0.04650 3.077 0.00376 **
diff_gov.l3 0.37446 0.18893 1.982 0.05438 .
diff_hh.l3 0.02682 0.05061 0.530 0.59910
const 0.02261 0.02707 0.835 0.40849
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.07446 on 40 degrees of freedom
Multiple R-Squared: 0.3626, Adjusted R-squared: 0.267
F-statistic: 3.792 on 6 and 40 DF, p-value: 0.004396
Estimation results for equation diff_hh:
========================================
diff_hh = diff_gov.l1 + diff_hh.l1 + diff_gov.l2 + diff_hh.l2 + diff_gov.l3 + diff_hh.l3 + const
Estimate Std. Error t value Pr(>|t|)
diff_gov.l1 1.403165 0.501766 2.796 0.0079 **
diff_hh.l1 0.007256 0.153957 0.047 0.9626
diff_gov.l2 -1.548307 0.630178 -2.457 0.0184 *
diff_hh.l2 0.057511 0.150394 0.382 0.7042
diff_gov.l3 1.294856 0.611078 2.119 0.0404 *
diff_hh.l3 -0.238964 0.163701 -1.460 0.1522
const 0.212912 0.087541 2.432 0.0196 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.2408 on 40 degrees of freedom
Multiple R-Squared: 0.2527, Adjusted R-squared: 0.1406
F-statistic: 2.254 on 6 and 40 DF, p-value: 0.05745
Covariance matrix of residuals:
diff_gov diff_hh
diff_gov 0.005544 0.003397
diff_hh 0.003397 0.057993
Correlation matrix of residuals:
diff_gov diff_hh
diff_gov 1.0000 0.1894
diff_hh 0.1894 1.0000
When you're interested in the structure of an object, so that you can extract particular elements, you can use str(x), e.g. str(summary(VAR(b, p = 3, type = "const"))).
For varest objects produced by vars::VAR, you can access the coefficients, p-values, etc. for a particular equation by accessing summary(m)$varresult$eq$coefficients, where m is the varest object, and eq is the variable of interest (diff_gov in your case).
Here's an example:
library(vars)
data(Canada)
m <- VAR(Canada, p = 2, type = "none")
summary(m)$varresult$e$coefficients
# Estimate Std. Error t value Pr(>|t|)
# e.l1 1.62046761 0.15483879 10.4655148 3.011899e-16
# prod.l1 0.17973134 0.06295812 2.8547760 5.584192e-03
# rw.l1 -0.04425592 0.05652496 -0.7829448 4.361581e-01
# U.l1 0.11310425 0.19947288 0.5670157 5.724195e-01
# e.l2 -0.64815156 0.15207587 -4.2620276 5.893211e-05
# prod.l2 -0.11683270 0.06797209 -1.7188334 8.982575e-02
# rw.l2 0.04475537 0.05472427 0.8178341 4.160775e-01
# U.l2 -0.06581206 0.19724901 -0.3336496 7.395876e-01
The resulting object is a matrix that can be subsetted normally. For example, to extract the estimate associated with rw.l1, you can use:
summary(m)$varresult$e$coefficients['rw.l1', 'Estimate']
[1] -0.04425592
So, in your case, you want something like this:
m <- VAR(b, p = 3, type = "const")
summary(m)$varresult$diff_gov$coefficients['diff_gov.l3', 'Estimate']

How to extract p-value in var package?

How can we extract p-value in var package. When we write summary(var), where 'var' is the name of var model, we see p-value at the bottom the results, but how we can extract this value?
For example:
library(vars)
symbols=c('^N225','^FTSE','^GSPC')
getSymbols(symbols,src='yahoo', from="2003-04-28", to="2007-10-29")
period="daily"
A1=periodReturn(N225$N225.Adjusted,period=period)
B1=periodReturn(FTSE$FTSE.Adjusted,period=period)
C1=periodReturn(GSPC$GSPC.Adjusted,period=period)
datap_1<-cbind(A1,B1,C1)
datap_1<-na.omit(datap_1)
datap_1<-(datap_1)^2
vardatap_3<-VAR(datap_1,p=3,type="none")
summary(vardatap_3)
after summary(vardatap_3) we can see the p-value, like this:
VAR Estimation Results:
=========================
Endogenous variables: N225, FTSE, SP500
Deterministic variables: none
Sample size: 1055
Log Likelihood: 23637.848
Roots of the characteristic polynomial:
0.8639 0.6224 0.6224 0.5711 0.5711 0.5471 0.5471 0.4683 0.4683
Call:
VAR(y = datap_1, p = 3, type = "none")
Estimation results for equation N225:
=====================================
N225 = N225.l1 + FTSE.l1 + SP500.l1 + N225.l2 + FTSE.l2 + SP500.l2 + N225.l3 + FTSE.l3 + SP500.l3
Estimate Std. Error t value Pr(>|t|)
N225.l1 0.03436 0.03116 1.103 0.270
FTSE.l1 0.47025 0.06633 7.089 2.48e-12 ***
SP500.l1 0.60717 0.07512 8.083 1.74e-15 ***
N225.l2 0.14938 0.03057 4.886 1.19e-06 ***
FTSE.l2 -0.05440 0.06744 -0.807 0.420
SP500.l2 -0.09024 0.07782 -1.160 0.246
N225.l3 0.16809 0.02924 5.749 1.18e-08 ***
FTSE.l3 0.04480 0.06597 0.679 0.497
SP500.l3 -0.01007 0.07941 -0.127 0.899
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.0002397 on 1046 degrees of freedom
Multiple R-Squared: 0.3099, Adjusted R-squared: 0.304
F-statistic: 52.2 on 9 and 1046 DF, p-value: < 2.2e-16
at the end of thi output, we see that p-value is less than 2.2e-16.
when I run this code:
lapply(coef(vardatap_3), "[", , "Pr(>|t|)"))
the output is:
$N225
N225.l1 FTSE.l1 SP500.l1 N225.l2 FTSE.l2 SP500.l2
2.703965e-01 2.479333e-12 1.738649e-15 1.189843e-06 4.201011e-01 2.464906e-01
N225.l3 FTSE.l3 SP500.l3
1.177588e-08 4.971743e-01 8.990626e-01
$FTSE
N225.l1 FTSE.l1 SP500.l1 N225.l2 FTSE.l2 SP500.l2
8.849041e-01 2.730359e-09 3.415860e-10 8.673114e-01 5.232037e-02 2.887330e-10
N225.l3 FTSE.l3 SP500.l3
8.698535e-02 6.215429e-15 9.290871e-02
$SP500
N225.l1 FTSE.l1 SP500.l1 N225.l2 FTSE.l2 SP500.l2
2.431252e-01 3.928462e-02 4.362288e-02 1.007840e-01 1.141799e-01 8.819460e-03
N225.l3 FTSE.l3 SP500.l3
1.129084e-03 1.426315e-01 1.307562e-06
and it's not the p-value. How can I reach to this value?
If fit is the object returned by the VAR function, you can use
lapply(coef(fit), "[", , "Pr(>|t|)")
to generate a list of vectors of the p-values.
If you want to extract/calculate the p-values for the whole models, you can try
sapply(summary(fit)$varresult, function(x) {
tmp <- x[["fstatistic"]]
pf(tmp[1], tmp[2], tmp[3], lower.tail = FALSE)
})

Resources