xiongweiaiyowei
l的喂诶
采纳率0%
2019-09-23 08:10

报错 Error in if (tmplam1 == Inf && tmplam2 == Inf) lambdavar[j] <- Inf else if (tmplam1 < : missing value where TRUE/FALSE needed 该怎么解决?

在ECONOMETRICS上读到一篇新的方法,想去实现这个代码

现在跑一个TENET的程序(具体程序代码见网页https://github.com/QuantLet/TENET/tree/master/TENET_SIM)

目前遇到问题见图片,报错说missing value where TRUE/FALSE needed ,第一次接触R,不太懂这个意思是什么,该怎么调试。

目前我企业数量是81家,有6个宏观变量,3个公司特征变量,想实现上述网页上的代码应该怎么做?求大神指导

PS:程序代码与数据我已经用连接的形式上传(我在经管之家上提问的网址https://bbs.pinggu.org/forum.php?mod=viewthread&tid=7334987&extra=)

图片说明

主代码附上,(TENET_SIMqrL1.r")与("quantilelasso.r") 这两个子代码在我经管之家链接上有附

Step 1: The main code of TENET based on quantile regression for Single-Index

Model with Variable selection technique

clear all variables

rm(list = ls(all = TRUE))
graphics.off()

set the working directory

setwd('C:/Users/wei xiong/Desktop/Rcode')

install and load packages

libraries = c("quantreg", "KernSmooth", "SparseM", "MASS")
lapply(libraries, function(x) if (!(x %in% installed.packages())) {
install.packages(x)
})
lapply(libraries, library, quietly = TRUE, character.only = TRUE)
source("TENET_SIMqrL1.r")
source("quantilelasso.r")

read the file which includes log returns of 81 firms and 6 macro state

variables

x0 = read.csv("81_firms_returns_and_macro.csv", header = TRUE)
date = x0[,1]
x0 = x0[,-1]
rownames(x0) = date

all firms' characteristics from balance sheet information of 81 firms

Bal_sheet_full = read.csv(file = "Bal_sheet810.csv")
date = Bal_sheet_full[,1]
Bal_sheet_full = Bal_sheet_full[,-1]
rownames(Bal_sheet_full) = date

6 macro state variables

m = as.matrix(x0[, 82:87])

estimated Value at Risk of 81 firms

VaR = as.matrix(read.csv("81VaR_movingwindows.csv"))[, 1:81]

log returns of 81 firms

xx0 = x0[, 1:81]

start the linear quantile lasso estimation for each firm

for (k in 1:81) {
cat("Firm:", k)
# log return of firm k
y = as.matrix(xx0[, k])
# log returns of firms except firm k
xx1 = as.matrix(xx0[, -k])
# 3 firm characteristics from balance sheet informaiton of firm k
BS = Bal_sheet_full[, (3 * k - 2):(3 * k)]

# combine macro state variables and 3 firm characteristics
MB = cbind(m, BS)
# number of rows of log return
n = nrow(xx1)
# number of covariates
p = ncol(xx1) + ncol(MB)
# estimated Value at Risk of firms except firm k
V = as.matrix(VaR[, -k])
# quantile level
tau = 0.05
# moving window size equals 12 corresponds to one year weekly data**_
ws = 12

lambda_sim = matrix(0, (n - ws), 1)
beta_sim = matrix(0, (n - ws), p)
covar_sim = matrix(0, (n - ws), 1)
first_der = matrix(0, (n - ws), 1)
partial_der = matrix(0, (n - ws), p)

for (l in 1:(n - ws)) {
print(l)
yw = y[l:(l + ws)]
MBw = MB[l:(l + ws), ]
mb = matrix(0, ws + 1, ncol(MB))
# standardize macro state variables and 3 firm characteristics
for (j in 1:ncol(MB)) {
mb[, j] = (MBw[, j] - min(MBw[, j]))/(max(MBw[, j]) - min(MBw[, j]))
}
mb[is.na(mb)] = 0
MBw[is.na(MBw)] = 0
xx = xx1[l:(l + ws), ]
# all the independent variables
xxw = cbind(xx, mb)
VaRM_est = as.numeric(c(V[l, ], mb[(ws + 1), ]))
#在此行报错
fit = sim(yw, xxw, tau, Qmaxiter = 2, l, k, LVaRest = VaRM_est)

beta_sim[l, ]    = fit$beta_final
lambda_sim[l]    = fit$lambda.fi
# the final estimated CoVaR
covar_sim[l]     = fit$a.fi
first_der[l]     = fit$b.fi
# the estimated partial derivatives
partial_der[l, ] = fit$c.fi

}
write.csv(lambda_sim, file = paste("lambda_sim_", k, ".csv", sep = ""))
write.csv(beta_sim, file = paste("beta_sim_", k, ".csv", sep = ""))
write.csv(covar_sim, file = paste("covar_sim_", k, ".csv", sep = ""))
write.csv(first_der, file = paste("first_der_", k, ".csv", sep = ""))
write.csv(partial_der, file = paste("partial_der_", k, ".csv", sep = ""))
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答