私は、次のコードを持っている:私の機能の簡素化
顕著であるとして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-mean)/ sdとして定義されます。
この場合、xは非定常な確率変数であるという事実を考慮しています。したがって、測定値は繰り返し測定する必要があり、観察回数にわたって繰り返し測定する必要があります。
dfは、ローリング線形モデルを推定するために使用されるウィンドウ長であり、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)
どのように私は以下の試験対策と私の機能を実行すると
は、私はエラーを取得していますこのエラーを修正できますか?また、私のコードを単純化する方法はありますか?
ありがとうございます。