0
我有以下代碼:簡化我的功能
z7 <- function(data, k, e){
require(zoo)
df = data
r = df$ROA
t = df$t
EA = df$EA
k = k
e = e
#Estimate rolling linear models
models = rollapply(df, width = k, FUN = function(z)
coef(lm(r~t, data = as.data.frame(z))), by.column = FALSE, align ="right")
#Extract residuals from the models
res = rollapply(df, width= k, FUN = function(x)
residuals(lm(r~t, data = as.data.frame(x))), by.column = FALSE, align ="right")
#Standard deviation and Mean of residuals, on a row basis
s = as.data.frame(apply(res, 1, sd))
m = as.data.frame(apply(res, 1, mean)) #note that this is aproximately 0 due to detrending.
#Combine the data define n as number of rows in the dataset
dataset = cbind(models, res, m, s)
n = as.vector(nrow(dataset))
n
dataset
#Compute predictions at k+1
for(i in n){
x = k + 1
preds = dataset$`(Intercept)` + dataset$t*(x)
x = x + 1
}
#Compute coefficient of variation
for(j in n){
n2 = k +1
tau = ((1 + 1/(4*(n2))) * (dataset$apply.res..1..sd./dataset$apply.res..1..mean.))
}
dataset3 = cbind(dataset, tau)
dataset3
#Compute mean of chi distribution and the adjusted standard deviation
Mchi <- sqrt(2)*((gamma((k+1)/2))/gamma(k/2))
S = s*Mchi*(k+1)/sqrt(k)
#Compute z7, checking whether the adjusted sd or cv should be used
for(i in nrow(dataset3)){
if (abs(dataset3$tau*dataset3$preds) < e) {
z = -(dataset3$EA + dataset3$preds)/S
} else
z = -(dataset3$EA + dataset3$preds) /(dataset3$tau*dataset3$preds)
}
}
由於是明顯的,我創建創建一個調整的標準化得分的功能。通常,Z分數被定義爲(x-均值)/ sd。
在這種情況下,我們考慮到x是一個非平穩隨機變量的事實。因此,必須在滾動基礎上估算該度量,並在觀察次數上進行迭代構建。
df是感興趣的數據集,k是用於估計軋製線性模型的窗口長度,而e是簡單地用於測試調整的標準偏差是否太小使用變異係數,而不是一個一個值替代標準差是針對異方差性進行調整的。
t = seq(0,15,1)
r = (100+50*sin(0.8*t))
EA = rnorm(0:15)
df = data.frame(t,r,EA)
test = z7(df, 3, 0.00000000001)
的錯誤是::
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 14, 0
回溯是:
5.
stop(gettextf("arguments imply differing number of rows: %s",
paste(unique(nrows), collapse = ", ")), domain = NA)
4.
data.frame(..., check.names = FALSE)
3.
cbind(deparse.level, ...)
2.
cbind(dataset, tau)
1.
z7(df, 3, 1e-11)
如何
運行我的功能與下面的測試措施,我得到一個錯誤我可以修復這個錯誤嗎?另外,有沒有辦法簡化我的代碼?
謝謝。