나는 fUnitroots의 unitrootTest에 따라 함수를 작성했습니다 :R에서 새 함수를 정의 할 때 루트 함수의 슬롯 (우리의 쓰여진 슬롯 제외)을 보호하는 방법은 무엇입니까?
내가 unitrootTest에서 온 한 외에 새로운 슬롯을 추가하고 사용할:
내가 비록, 슬롯 @Test 추가library(fUnitRoots)
adfcs1 <- function(t, max = floor(12*(length(t)/100)^(1/4)), type = c("c")) {
x <- ts(t)
x1d <- diff(x, differences=1)
x1l <- lag(x, -1)
x_names <- c("x1d", "x1l", sapply(1:max, function(i) paste("x1d", i, "l", sep="")))
for (i in as.integer(1:max)) { assign(x_names[i+2], lag(x1d, -i)) }
DLDlag <- do.call(ts.intersect, sapply(x_names, as.symbol))
DLDlag.df <- data.frame(DLDlag, obspts = c(time(DLDlag)))
DifferenceLags <- as.vector(names(DLDlag.df), mode="any")[3: (length(DLDlag.df)-1)]
lmresults <- array(list())
SBCvalues <- array(list())
AICvalues <- array(list())
for (i in as.integer(0:max)) {
if (type==c("nc")) {
if (i == 0) { lmresults[[max+1]] <- lm(as.formula(paste("x1d ~x1l")),data=DLDlag.df)
SBCvalues[[max+1]] <- BIC(lmresults[[max+1]])
AICvalues[[max+1]] <- AIC(lmresults[[max+1]]) }
if (i > 0) { lmresults[[i]] <- lm(as.formula(paste("x1d ~ x1l+", paste(DifferenceLags[1:i], collapse="+"))),data=DLDlag.df)
SBCvalues[[i]] <- BIC(lmresults[[i]])
AICvalues[[i]] <- AIC(lmresults[[i]]) }
}
if (type==c("c")) {
if (i == 0) { lmresults[[max+1]] <- lm(as.formula(paste("x1d ~1+x1l")),data=DLDlag.df)
SBCvalues[[max+1]] <- BIC(lmresults[[max+1]])
AICvalues[[max+1]] <- AIC(lmresults[[max+1]]) }
if (i > 0) { lmresults[[i]] <- lm(as.formula(paste("x1d ~ 1+x1l+", paste(DifferenceLags[1:i], collapse="+"))),data=DLDlag.df)
SBCvalues[[i]] <- BIC(lmresults[[i]])
AICvalues[[i]] <- AIC(lmresults[[i]]) }
}
if (type==c("ct")) {
if (i == 0) { lmresults[[max+1]] <- lm(as.formula(paste("x1d ~ 1+x1l+seq_along(x1d)",collapse="")),data=DLDlag.df)
SBCvalues[[max+1]] <- BIC(lmresults[[max+1]])
AICvalues[[max+1]] <- AIC(lmresults[[max+1]]) }
if (i > 0) { lmresults[[i]] <- lm(as.formula(paste("x1d ~ 1+x1l+seq_along(x1d)+",paste(DifferenceLags[1:i], collapse="+"))),data=DLDlag.df)
SBCvalues[[i]] <- BIC(lmresults[[i]])
AICvalues[[i]] <- AIC(lmresults[[i]]) }
}
}
list(which.min(SBCvalues), which.min(AICvalues))
as.data.frame(cbind(SBCvalues, AICvalues))
typespecified <- type
if (which.min(SBCvalues)==max+1) {
scs <- (max+2)-(0+1)
adfcs1 <- unitrootTest(x[scs:length(x)], lags = 0, type = typespecified)
} else {
scs <- (max+2)-(which.min(SBCvalues)+1)
adfcs1 <- unitrootTest(x[scs:length(x)], lags =which.min(SBCvalues), type = typespecified)
}
adfcs1
}
나는 (내가 원하는대로) 자동 unitrootTest에서 상세 "계수"결과를 검색, adfcs1에서 정의하지 않았다
> adfcs1(f1f.zs,max=1,type="c")@test
$data.name
[1] "x[scs:length(x)]"
$regression
Call:
lm(formula = y.diff ~ y.lag.1 + 1)
Residuals:
Min 1Q Median 3Q Max
-2.49700 -0.12039 0.06813 0.26813 0.77105
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.05766 0.04631 -1.245 0.216
y.lag.1 -0.47631 0.08387 -5.679 1.36e-07 ***
.....
$statistic
DF
-5.679303
$p.value
t n
4.216696e-06 3.641807e-01
$parameter
Lag Order
0
나는 외에 내 정의 슬롯 (optmins, SBCAIC 등)을 사용할
그 하나 unitrootTest에서 왔을 때, @test slot을 추가하면 에러가납니다.adfcs2 <- function(t, max = floor(12*(length(t)/100)^(1/4)), type = c("c")) {
x <- ts(t)
x1d <- diff(x, differences=1)
x1l <- lag(x, -1)
x_names <- c("x1d", "x1l", sapply(1:max, function(i) paste("x1d", i, "l", sep="")))
for (i in as.integer(1:max)) { assign(x_names[i+2], lag(x1d, -i)) }
DLDlag <- do.call(ts.intersect, sapply(x_names, as.symbol))
DLDlag.df <- data.frame(DLDlag, obspts = c(time(DLDlag)))
DifferenceLags <- as.vector(names(DLDlag.df), mode="any")[3: (length(DLDlag.df)-1)]
lmresults <- array(list())
SBCvalues <- array(list())
AICvalues <- array(list())
for (i in as.integer(0:max)) {
if (type==c("nc")) {
if (i == 0) { lmresults[[max+1]] <- lm(as.formula(paste("x1d ~x1l")),data=DLDlag.df)
SBCvalues[[max+1]] <- BIC(lmresults[[max+1]])
AICvalues[[max+1]] <- AIC(lmresults[[max+1]]) }
if (i > 0) { lmresults[[i]] <- lm(as.formula(paste("x1d ~ x1l+", paste(DifferenceLags[1:i], collapse="+"))),data=DLDlag.df)
SBCvalues[[i]] <- BIC(lmresults[[i]])
AICvalues[[i]] <- AIC(lmresults[[i]]) }
}
if (type==c("c")) {
if (i == 0) { lmresults[[max+1]] <- lm(as.formula(paste("x1d ~1+x1l")),data=DLDlag.df)
SBCvalues[[max+1]] <- BIC(lmresults[[max+1]])
AICvalues[[max+1]] <- AIC(lmresults[[max+1]]) }
if (i > 0) { lmresults[[i]] <- lm(as.formula(paste("x1d ~ 1+x1l+", paste(DifferenceLags[1:i], collapse="+"))),data=DLDlag.df)
SBCvalues[[i]] <- BIC(lmresults[[i]])
AICvalues[[i]] <- AIC(lmresults[[i]]) }
}
if (type==c("ct")) {
if (i == 0) { lmresults[[max+1]] <- lm(as.formula(paste("x1d ~ 1+x1l+seq_along(x1d)",collapse="")),data=DLDlag.df)
SBCvalues[[max+1]] <- BIC(lmresults[[max+1]])
AICvalues[[max+1]] <- AIC(lmresults[[max+1]]) }
if (i > 0) { lmresults[[i]] <- lm(as.formula(paste("x1d ~ 1+x1l+seq_along(x1d)+",paste(DifferenceLags[1:i], collapse="+"))),data=DLDlag.df)
SBCvalues[[i]] <- BIC(lmresults[[i]])
AICvalues[[i]] <- AIC(lmresults[[i]]) }
}
}
out <- list()
out$optmins <- list(which.min(SBCvalues), which.min(AICvalues))
out$SBCAIC <- as.data.frame(cbind(SBCvalues, AICvalues))
typespecified <- type
if (which.min(SBCvalues)==max+1) {
scs <- (max+2)-(0+1)
out$adfcst <- unitrootTest(x[scs:length(x)], lags = 0, type = typespecified)
} else {
scs <- (max+2)-(which.min(SBCvalues)+1)
out$adfcst <- unitrootTest(x[scs:length(x)], lags =which.min(SBCvalues), type = typespecified)
}
out
}
> adfcs2(t1f.zs,max=1,type="c")$optmins # My defined slot works
[[1]]
[1] 2
[[2]]
[1] 2
> adfcs2(t1f.zs,max=1,type="c")@test
# Whereas this time original slot that (I guess) came from unitrootTest doesn't work anymore!
Error: trying to get slot "test" from an object of a basic class ("list") with no slots
새 기능에 정의 된 것 외에 루트 기능에서 나온 슬롯을 유지하는 방법은 무엇입니까? $와 @를 제대로 사용하지 못했습니다. $는 무엇입니까? 무엇을 위해서입니까?
도움이 될 것입니다.
함수를주의 깊게 읽지는 않았지만 함수가 표준'list' 객체를 반환하고 슬롯이없는 것으로 보입니다. 'unirootTest'가'test' 슬롯을 가진'S4' 객체를 반환하면,'adfcs2 (t1f.zs, max = 1, type = "c") $ adfcst @ test'를 통해 그것을 검색 할 수 있어야합니다. – nicola