Building time series from lists in R? - r

Hi I am trying to make a list of time-series objects by applying ts() function to the list of dataframes that I have. My list of dataframes is called whisk.basic.lst and it contain 69 dataframes. I am showing how it look like below:
> str(whisk.basic.lst)
$ SI_VALUES_IMV_EU28_INTRA :List of 12
..$ 220830 : num [1:74] 218674 255317 327377 363219 335267 ...
..$ 22083011: num [1:74] 9363 10129 19672 20631 10403 ...
..$ 22083019: num [1:74] 0 1978 0 0 7116 ...
..$ 22083030: num [1:74] 3837 15684 14588 20487 30870 ...
..$ 22083041: num [1:74] 18979 5123 7176 36842 9390 ...
..$ 22083049: num [1:74] 688 0 0 0 0 0 0 0 0 0 ...
..$ 22083061: num [1:74] 0 0 3452 4225 96 ...
..$ 22083069: num [1:74] 0 0 0 40 0 0 7520 0 0 0 ...
..$ 22083071: num [1:74] 139915 204803 256095 218105 185088 ...
..$ 22083079: num [1:74] 0 3219 0 0 3381 ...
..$ 22083082: num [1:74] 45892 14381 26394 62889 88527 ...
..$ 22083088: num [1:74] 0 0 0 0 396 0 0 0 642 105 ...
$ SK_VALUES_IMV_EU28_EXTRA :List of 11
..$ 220830 : num [1:74] 7155 12311 237 2705 7419 ...
..$ 22083011: num [1:74] 0 0 0 0 35 0 0 0 122 0 ...
..$ 22083019: num [1:74] 0 0 0 0 0 0 0 0 0 0 ...
..$ 22083030: num [1:74] 0 0 0 0 935 235 732 0 669 0 ...
..$ 22083041: num [1:74] 0 0 0 0 2603 ...
..$ 22083049: num [1:74] 0 0 0 0 0 0 0 0 0 0 ...
..$ 22083061: num [1:74] 0 0 0 0 0 0 0 0 0 0 ...
..$ 22083071: num [1:74] 0 0 0 0 27 0 546 0 0 0 ...
..$ 22083079: num [1:74] 0 0 0 0 0 0 0 0 0 0 ...
..$ 22083082: num [1:74] 7155 12311 237 2705 3819 ...
..$ 22083088: num [1:74] 0 0 0 0 0 0 0 0 0 0 ...
$ SK_VALUES_IMV_EU28_INTRA :List of 11
..$ 220830 : num [1:74] 380007 459653 155033 205879 297446 ...
..$ 22083011: num [1:74] 26772 68577 8585 24567 17996 ...
..$ 22083019: num [1:74] 0 0 0 0 0 0 0 0 0 0 ...
..$ 22083030: num [1:74] 60521 15068 1830 9788 5557 ...
..$ 22083041: num [1:74] 110461 71551 66317 47354 46776 ...
..$ 22083049: num [1:74] 100 19718 4115 201 201 ...
..$ 22083061: num [1:74] 0 29706 0 0 8177 ...
..$ 22083071: num [1:74] 418 21760 3138 68164 46300 ...
..$ 22083079: num [1:74] 0 738 0 738 2213 ...
..$ 22083082: num [1:74] 181487 186179 68737 53360 170226 ...
..$ 22083088: num [1:74] 248 46356 2311 1707 0 ...
Ts.whisk.lst <- lapply(whisk.basic.lst, function(x) ts(x, frequency= 12, start=c(2010,1)))
I am getting the following error:
Error in ts(x, frequency = 12, start = c(2010, 1)) :
'ts' object must have one or more observations
Called from: ts(x, frequency = 12, start = c(2010, 1))
I have been creating time series from my dataframes without any problem in the past. But since I created a list of these dataframes, I have run into this problem. Does anyone know why?

Related

Combining elements of different lengths in matching lists in R

I am trying to combine information from two corresponding lists, both of 4. The first list only contains 1 element for each of the 4 items, and is of the following structure:
List of 4
$ :'data.frame': 8640 obs. of 3 variables:
..$ x : num [1:8640] -108 -108 -108 -107 -107 ...
..$ y : num [1:8640] 25.9 25.9 25.9 25.9 25.9 ...
..$ layer: num [1:8640] 0 0 0 0 0 0 0 0 0 0 ...
$ :'data.frame': 20520 obs. of 3 variables:
..$ x : num [1:20520] -116 -116 -116 -115 -115 ...
..$ y : num [1:20520] 32.9 32.9 32.9 32.9 32.9 ...
..$ layer: num [1:20520] 0.002 0 0 0 0 ...
$ :'data.frame': 13500 obs. of 3 variables:
..$ x : num [1:13500] -112 -112 -112 -111 -111 ...
..$ y : num [1:13500] 29.9 29.9 29.9 29.9 29.9 ...
..$ layer: num [1:13500] 0.00583 0.01166 0.01749 0.01749 0.01749 ...
$ :'data.frame': 15300 obs. of 3 variables:
..$ x : num [1:15300] -117 -117 -117 -116 -116 ...
..$ y : num [1:15300] 31.9 31.9 31.9 31.9 31.9 ...
..$ layer: num [1:15300] 0 0 0 0 0 0 0 0 0 0 ...
I have another list that is also of 4, where I want to add the data in that list as 2 separate columns to the dataframes in their corresponding elements in the first list.
The structure of this second list is as follows:
List of 4
$ : chr [1:2] "green" "0.00689301"
$ : chr [1:2] "blue" "0.01291301"
$ : chr [1:2] "red" "0.02905452"
$ : chr [1:2] "black" "0.00879968"
Basically, I want a new list that has the following structure in each of the 4 members of the list:
List of 4
$ :'data.frame': 8640 obs. of 3 variables:
..$ x : num [1:8640] -108 -108 -108 -107 -107 ...
..$ y : num [1:8640] 25.9 25.9 25.9 25.9 25.9 ...
..$ layer: num [1:8640] 0 0 0 0 0 0 0 0 0 0 ...
..$ color: chr [1:8640] "green" "green" "green"...
..$ value: chr [1:8640] "0.00689301" "0.00689301" ...
$ :'data.frame': 20520 obs. of 3 variables:
..$ x : num [1:20520] -116 -116 -116 -115 -115 ...
..$ y : num [1:20520] 32.9 32.9 32.9 32.9 32.9 ...
..$ layer: num [1:20520] 0.002 0 0 0 0 ...
..$ color: chr [1:20520] "blue" "blue" "blue" ...
..$ value: chr [1:20520] "0.01291301" "0.01291301" ...
I have tried combining this information all at once using mapply() and c(), but that didn't give me the flexibility to change these new members from my second list from individual values into vectors of the same length as the corresponding new list.
We may use Map
Map(function(x, y) transform(x, color = y[1], value = y[2]), lst1, lst2)
# or another way is
Map(function(x, y) {x[c("color", "value")] <- as.list(y); x}, lst1, lst2)
Or with map2
library(purrr)
library(dplyr)
map2(lst1, lst2, ~ .x %>%
mutate(color = .y[1], value = .y[2]))

issue with gbm.step() function in R

I'm trying to execute the Cross-Validation for the boosting regression/classification trees using the function gbm.step() from the R package dismo, but it returns a empty output and I can't figure out why. This is the code I'm using:
ColIndexCov <- match(names(myRS),colnames(DFbrt_df2))
ColIndexResp <- match(c("HasRes"),colnames(DFbrt_df2))
DFbrt_df <- DFbrt#data
DFbrt_df2 <- na.omit(DFbrt_df)
myBRT = gbm.step(data=DFbrt_df2,
gbm.x = ColIndexCov,
gbm.y = ColIndexResp,
tree.complexity = 3,
learning.rate = 10^(-8),
n.trees = 50,
family = "bernoulli",
n.folds = 4,
fold.vector = DFbrt_df2$Region.num,
step.size = 50,
verbose = F,
silent = T
)
str(DFbrt_df2)
'data.frame': 560845 obs. of 18 variables:
$ Nsamples : num 310 310 310 310 310 310 310 310 310 310 ...
$ cluster : num 39 39 39 39 39 39 39 39 39 39 ...
$ R : num 44.9 44.9 44.9 44.9 44.9 ...
$ P50 : num 0.565 0.544 0.609 0.605 0.593 ...
$ regions : Factor w/ 6 levels "China_east","China_middlesouth",..: 1 1 1 1 1 1 1 1 1 1 ...
$ HasRes : num 1 0 1 0 0 0 1 1 0 0 ...
$ use : num 10.02 9.75 0 9.38 8.77 ...
$ acc : num 0 0 0.4103 0.0769 0.0779 ...
$ tmp : num 2.46 2.46 2.46 2.46 2.45 ...
$ irg : num 1.788 0.399 1.205 1.836 1.841 ...
$ PgExt : num 3.11 0 3.7 3.11 3.18 ...
$ PgInt : num 4.69 2.76 0 3.99 2.22 ...
$ ChExt : num 3.74 0 4.33 3.74 3.81 ...
$ ChInt : num 5.01 5.99 5.35 4.88 4.97 ...
$ Ca : num 0 0 2.71 0 2.8 ...
$ veg : num 0 0 0 0 0 0 0 0 0 0 ...
$ Region.num: num 4 4 4 4 4 4 4 4 4 4 ...
$ Region : num 4 4 4 4 4 4 4 4 4 4 ...
- attr(*, "na.action")= 'omit' Named int 1 2 3 4 5 6 7 8 9 10 ...
..- attr(*, "names")= chr "1" "2" "3" "4" ...
the answer variable is the variable HasRes and the covariates are the variables use, acc, tmp, irg, PgExt, PgInt, ChExt, ChInt, ca, veg.

Loop through list programatically

I have a list in R that I want to loop through all the elements.
This is the structure of the object:
> str(AAPL.OPT[c])
List of 1
$ jun.12.2020:List of 2
..$ calls:'data.frame': 52 obs. of 7 variables:
.. ..$ Strike: num [1:52] 180 185 200 210 240 ...
.. ..$ Last : num [1:52] 123 118 131 120 85 ...
.. ..$ Chg : num [1:52] 0 0 7.61 9.48 0 ...
.. ..$ Bid : num [1:52] 149 144 129 119 89 ...
.. ..$ Ask : num [1:52] 153.3 148.5 133.5 123.7 93.5 ...
.. ..$ Vol : int [1:52] NA 15 16 2 1 1 3 36 1 2 ...
.. ..$ OI : int [1:52] 0 15 25 4 50 3 4 36 6 10 ...
..$ puts :'data.frame': 56 obs. of 7 variables:
.. ..$ Strike: num [1:56] 150 165 170 180 185 190 195 200 205 210 ...
.. ..$ Last : num [1:56] 0.05 0.02 0.14 0.05 0.03 0.02 0.01 0.02 0.01 0.01 ...
.. ..$ Chg : num [1:56] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ Bid : num [1:56] NA 0 0 0 0 0 0 0 0 0 ...
.. ..$ Ask : num [1:56] 2.13 0.11 0.11 1.8 1.87 0.01 1.88 0.5 1.88 2.13 ...
.. ..$ Vol : int [1:56] NA 1 1 2 1 16 1 17 1 21 ...
.. ..$ OI : int [1:56] 1 10 7 9 76 201 113 314 92 264 ...
I cannot access the next level of the object programatically (by indexing the value)
I want to do something like this:
AAPL.OPT[c][1]
instead of this
AAPL.OPT[c]$jun.12.2020
Sample data of AAPL.OPT[c]
$`jun.12.2020`$`calls`
Strike Last Chg Bid Ask Vol OI
AAPL200612C00180000 180.0 123.29 0.00000000 149.00 153.35 NA 0
AAPL200612C00185000 185.0 117.60 0.00000000 144.00 148.50 15 15
AAPL200612C00200000 200.0 131.15 7.60999300 129.00 133.50 16 25
AAPL200612C00210000 210.0 119.95 9.47999600 119.30 123.65 2 4
....
AAPL.OPT[c] gives a list of length 1 which has two other lists in them. If we use [[c]] it gives a list of length 2 andtTo access each dataframe you can subset them further using [[ so AAPL.OPT[[c]][[1]] and AAPL.OPT[[c]][[2]].
We can use
AAPL.OPT[[c]]$jun.12.2020

R Extract Data From SurvFit

library(survival)
etime <- with(mgus2, ifelse(pstat==0, futime, ptime))
event <- with(mgus2, ifelse(pstat==0, 2*death, 1))
event <- factor(event, 0:2, labels=c("censor", "pcm", "death"))
mfit2 <- survfit(Surv(etime, event) ~ sex, data=mgus2)
plot(mfit2, col=c(1,2,1,2), lty=c(2,2,1,1),
mark.time=FALSE, lwd=2, xscale=12,
xlab="Years post diagnosis", ylab="Probability in State")
legend(240, .6, c("death:female", "death:male", "pcm:female", "pcm:male"),
col=c(1,2,1,2), lty=c(1,1,2,2), lwd=2, bty='n')
This is a reproducible example here. I wonder, how can it be possible to take out these data from 'mfit2' so it can be plotted in ggplot2?
You can extract the data from the summary of the fitted object using lapply
sfit <- summary(mfit2)
str(sfit)
List of 24
$ n : int [1:2] 631 753
$ time : num [1:359] 1 2 3 4 5 6 7 8 9 10 ...
$ n.risk : int [1:359, 1:3] 631 610 599 595 588 587 581 580 573 569 ...
$ n.event : int [1:359, 1:3] 0 0 0 0 0 0 0 0 0 0 ...
$ n.censor : num [1:359] 1 0 0 0 0 0 0 0 0 1 ...
$ pstate : num [1:359, 1:3] 0.968 0.951 0.944 0.933 0.932 ...
$ p0 : num [1:2, 1:3] 1 1 0 0 0 0
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:2] "sex=F" "sex=M"
.. ..$ : chr [1:3] "(s0)" "pcm" "death"
$ strata : Factor w/ 2 levels "sex=F","sex=M": 1 1 1 1 1 1 1 1 1 1 ...
...
I think the columns you need are the time, pstate and `strata. But some others, such as the numbers at risk may be useful.
cols <- lapply(c(2:6, 8, 16, 17), function(x) sfit[x])
Then combine these columns into a data frame with do.call
data <- do.call(data.frame, cols)
str(data)
'data.frame': 359 obs. of 21 variables:
$ time : num 1 2 3 4 5 6 7 8 9 10 ...
$ n.risk.1 : int 631 610 599 595 588 587 581 580 573 569 ...
$ n.risk.2 : int 0 0 0 0 0 0 0 0 0 0 ...
$ n.risk.3 : int 0 0 0 0 0 0 0 0 0 0 ...
$ n.event.1: int 0 0 0 0 0 0 0 0 0 0 ...
$ n.event.2: int 0 2 0 1 0 1 0 0 2 1 ...
$ n.event.3: int 20 9 4 6 1 5 1 7 2 2 ...
$ n.censor : num 1 0 0 0 0 0 0 0 0 1 ...
$ pstate.1 : num 0.968 0.951 0.944 0.933 0.932 ...
$ pstate.2 : num 0 0.00317 0.00317 0.00476 0.00476 ...
$ pstate.3 : num 0.0317 0.046 0.0523 0.0619 0.0634 ...
$ strata : Factor w/ 2 levels "sex=F","sex=M": 1 1 1 1 1 1 1 1 1 1 ...
$ lower.1 : num 0.955 0.934 0.927 0.914 0.912 ...
$ lower.2 : num NA 0.000796 0.000796 0.00154 0.00154 ...
$ lower.3 : num 0.0206 0.0322 0.0375 0.0456 0.047 ...
$ upper.1 : num 0.982 0.968 0.963 0.953 0.952 ...
$ upper.2 : num NA 0.0127 0.0127 0.0147 0.0147 ...
$ upper.3 : num 0.0488 0.0656 0.0729 0.0838 0.0856 ...
This data is in wide format, best to reshape to long for the graph.
mgus3 <- data %>%
pivot_longer(cols=-c(time, strata, n.censor),
names_to=c(".value","state"),
names_pattern="(.+).(.+)") %>%
filter(state!=1) %>% # Exclude the censored state
mutate(state=factor(state, labels=c("pcm","death")),
group=interaction(strata, state))
Then plot it.
library(ggplot)
mgus3 %>%
ggplot(aes(x=time, y=pstate, col=group)) +
geom_line(aes(linetype=group)) +
ylab("Probability in State") +
theme_bw()
You should be able to add confidence bands and make it more pretty.

extract AIC from a fixed effect spatial panel model estimation result

I am using the package 'splm' of Millo and Piras(2012) to estimate a SPatial Durbin model with country (individual) fixed effect.
ks = log(spa.sak$index_ret+2) ~ log(spa.sak$lagindex+2) + log(spa.sak$cred_def_rate+2) + log(spa.sak$ch_in_ex_rate+2) +
log(spa.sak$un_inf+2) + log(spa.sak$gdp_growth+2) + log(spa.sak$cha_in_int_rate+2) + country
ERVM<- spml(formula = ks, data = spa.sak, index = c('id','months'), listw = ERV, model = "within", lag = TRUE, effect = "individual", spatial.error = "none")
> summary(ERVM)
Spatial panel fixed effects lag model
Call:
spml(formula = ks, data = spa.sak, index = c("id", "months"),
listw = ERV, model = "within", effect = "individual", lag = TRUE,
spatial.error = "none")
Residuals:
Min. 1st Qu. Median 3rd Qu. Max.
-0.783343 -0.028334 -0.003056 0.020040 1.199906
Spatial autoregressive coefficient:
Estimate Std. Error t-value Pr(>|t|)
lambda -0.0040442 0.0061785 -0.6546 0.5127
Coefficients:
Estimate Std. Error t-value Pr(>|t|)
log(spa.sak$lagindex + 2) -2.5577e-01 1.6593e-02 -15.4142 < 2.2e-16 ***
log(spa.sak$cred_def_rate + 2) 1.3427e-02 2.3244e-02 0.5777 0.5635
log(spa.sak$ch_in_ex_rate + 2) -5.1946e-04 2.2338e-03 -0.2325 0.8161
log(spa.sak$un_inf + 2) 1.1360e-01 2.2112e-01 0.5138 0.6074
log(spa.sak$gdp_growth + 2) 3.1093e+00 2.1339e+00 1.4571 0.1451
log(spa.sak$cha_in_int_rate + 2) -1.4148e+01 3.1507e+00 -4.4905 7.105e-06 ***
countryEGYPT 1.0659e-02 1.7940e-02 0.5942 0.5524
countryGHANA 2.3214e-02 2.0567e-02 1.1287 0.2590
countryKENYA 1.6923e-02 1.9599e-02 0.8634 0.3879
countryMALAWI 1.0190e-02 3.2028e-02 0.3182 0.7504
countryMAURITIUS 2.5843e-03 1.3530e-02 0.1910 0.8485
countryMOROCCO 4.2243e-03 1.4848e-02 0.2845 0.7760
countryNAMIBIA -1.0416e-02 1.4408e-02 -0.7229 0.4697
countryNIGERIA 1.2794e-02 1.8327e-02 0.6981 0.4851
countrySOUTH AFRICA -9.4671e-04 1.3665e-02 -0.0693 0.9448
countryTANZANIA 1.9698e-02 1.9822e-02 0.9937 0.3204
countryTUNISIA 1.4579e-02 1.4895e-02 0.9788 0.3277
countryUGANDA 3.1850e-02 2.1042e-02 1.5137 0.1301
countryZAMBIA 3.3671e-02 2.1202e-02 1.5881 0.1123
countryZIMBABWE 1.4299e-02 3.1798e-02 0.4497 0.6529
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> str(ERVM)
List of 15
$ coefficients : Named num [1:21] -0.004044 -0.255767 0.013427 -0.000519 0.113601 ...
..- attr(*, "names")= chr [1:21] "lambda" "log(spa.sak$lagindex + 2)" "log(spa.sak$cred_def_rate + 2)" "log(spa.sak$ch_in_ex_rate + 2)" ...
$ errcomp : NULL
$ vcov : num [1:21, 1:21] 3.82e-05 0.00 0.00 0.00 0.00 ...
$ spat.coef : Named num -0.00404
..- attr(*, "names")= chr "lambda"
$ vcov.errcomp : NULL
$ residuals : Named num [1:3390] -0.1195 0.0395 0.0151 -0.0186 0.0285 ...
..- attr(*, "names")= chr [1:3390] "5-01/01/2000" "5-01/01/2001" "5-01/01/2002" "5-01/01/2003" ...
$ fitted.values: num [1:3390, 1] 0.681 0.712 0.704 0.725 0.719 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:3390] "5-01/01/2000" "5-01/01/2001" "5-01/01/2002" "5-01/01/2003" ...
.. ..$ : NULL
..- attr(*, "names")= chr [1:3390] "5-01/01/2000" "5-01/01/2001" "5-01/01/2002" "5-01/01/2003" ...
$ sigma2 : num 0.0192
$ type : chr "fixed effects lag"
$ model :'data.frame': 3390 obs. of 21 variables:
..$ y : num [1:3390] 0.537 0.731 0.697 0.685 0.728 ...
..$ log.spa.sak.lagindex...2. : num [1:3390] 0.851 0.71 0.752 0.674 0.731 ...
..$ log.spa.sak.cred_def_rate...2. : num [1:3390] 2.83 2.83 2.83 2.83 2.83 ...
..$ log.spa.sak.ch_in_ex_rate...2. : num [1:3390] 0.696 0.69 0.711 0.768 0.695 ...
..$ log.spa.sak.un_inf...2. : num [1:3390] 0.697 0.694 0.693 0.692 0.694 ...
..$ log.spa.sak.gdp_growth...2. : num [1:3390] 0.693 0.693 0.693 0.693 0.693 ...
..$ log.spa.sak.cha_in_int_rate...2.: num [1:3390] 0.693 0.693 0.693 0.693 0.693 ...
..$ countryEGYPT : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryGHANA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryKENYA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryMALAWI : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryMAURITIUS : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryMOROCCO : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryNAMIBIA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryNIGERIA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countrySOUTH.AFRICA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryTANZANIA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryTUNISIA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryUGANDA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryZAMBIA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryZIMBABWE : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
$ call : language spml(formula = ks, data = spa.sak, index = c("id", "months"), listw = ERV, model = "within", effect = "individual| __truncated__
$ logLik : num 1895
$ method : chr "eigen"
$ effects : chr "spfe"
$ res.eff :List of 2
..$ :List of 7
.. ..$ res.sfe : num [1:15, 1] 0.00264 -0.00152 0.0017 0.0023 0.0106 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:15] "1" "2" "3" "4" ...
.. .. .. ..$ : NULL
.. ..$ res.se.sfe: Named num [1:15] 2.55 2.55 2.55 2.55 2.55 ...
.. .. ..- attr(*, "names")= chr [1:15] "1" "2" "3" "4" ...
.. ..$ intercept : num [1, 1] 8.43
.. ..$ res.se.con: num [1, 1] 2.55
.. ..$ xhat : num [1:3390, 1] 0.681 0.712 0.704 0.725 0.719 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:3390] "5-01/01/2000" "5-01/01/2001" "5-01/01/2002" "5-01/01/2003" ...
.. .. .. ..$ : NULL
.. ..$ N.vars : int 35
.. ..$ res.e : num [1:3390, 1] -0.1195 0.0395 0.0151 -0.0186 0.0285 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:3390] "5-01/01/2000" "5-01/01/2001" "5-01/01/2002" "5-01/01/2003" ...
.. .. .. ..$ : NULL
..$ res.corr: NULL
- attr(*, "class")= chr "splm"
I am successful to obtain estimation results except the following two.
1) As it is shown above, I am only able to get values for \lambda and \beta coefficients. I can not get the \theta estimation result or value that relates with the spatially lagged explanatory variables which shows the spatio-temporal relationships. How can extract it?
2) I am not able to extract AIC and BIC for model comparison.
In the first case, my trials weren't successful, and
For the second case, I have tried
AICsplm(ERVMX, criterion="AIC")
But I got an error.
How can I fix these issues?
UPDATE: I have solved the issue and please refer below.
Construct the spatial lag for both response and predictor variable in advance and add in to the formula and run.
As far as AIC is concerned, there is a need to download AICsplm.R from github download AICsplm.R to run the code.
source(AICsplm.R)
AICsplm(ERVM)
Based on the answer in this question, this function also works for splm objects:
AIC_adj <- function(mod){
# Number of observations
n.N <- nrow(mod$model)
# Residuals vector
u.hat <- residuals(mod)
# Variance estimation
s.sq <- log( (sum(u.hat^2)/(n.N)))
# Number of parameters (incl. constant) + one additional for variance estimation
p <- length(coef(mod)) + 1
# Note: minus sign cancels in log likelihood
aic <- 2*p + n.N * ( log(2*pi) + s.sq + 1 )
return(aic)
}

Resources