我正在尝试为R中的论文评估异构资产定价模型。但是,当我运行代码时,我得到了错误;
Error in nls(P ~ PfitHV(P, Y, r, g, Beta, Phi1, Phi2, Delta, W), start = start.val, :
Convergence failure: singular convergence (7)
我认为这可能与错误的起始值或某种类型的模型的过度参数化有关(基于关于同一错误的堆栈问题),但我不知道如何解决。我整日忙于此,所以任何帮助将不胜感激!
这是我的一些代码(很抱歉,但是我真的不知道要包括/排除什么):
memory <- T # Use memory in fractions
async.updating <- T # Use asyncronous updating (and estimate delta)
with.beta <- T # Estimate beta (or fix it)
# start values, lowerbound and upperbounds for phi1, delta_phi
start.val <- list(Phi1 = 0.2, Phi2 = 0.3)
lower.val <- list(Phi1 = 0.0, Phi2 = 0.0)
upper.val <- list(Phi1 = 1.5, Phi2 = 3.4)
Beta<-1
if (with.beta) {
start.val <- c(start.val, Beta = 1)
lower.val <- c(lower.val, Beta = 0)
upper.val <- c(upper.val, Beta = 1000)
}
Delta <- 0.0
if (async.updating) {
start.val <- c(start.val, Delta = 0.5)
lower.val <- c(lower.val, Delta = 0.0)
upper.val <- c(upper.val, Delta = 1.0)
}
W <- 0
if (memory) {
start.val <- c(start.val, W = 0.1)
lower.val <- c(lower.val, W = 0.0)
upper.val <- c(upper.val, W = 1.0)
}
##########################################################################################
PfitHV <- function(P, Y, r, g, Beta, Phi1, Phi2, Delta, W) {
T=NROW(P) #number of rows (observations)
L=NCOL(P) #number of columns
# creating lags of P, Y
P1 <- lag(P,k = 1)
P2 <- lag(P,k = 2)
Y1 <- lag(Y,k = 1)
Y2 <- lag(Y,k = 2)
# creation of Psi (in this version use static Gordon model)
Psi <- (1+g)/(r-g)
# payoffs of period t-1 -> equation 16 (without fraction in front)
Pi1 <- (P1+Y1-(1+r)*P2)*((Phi1*Psi+1)*Phi1*(1+g)*Y2-(1+r)*P2)
Pi2 <-(P1+Y1-(1+r)*P2)*((Phi2*Psi+1)*Phi2*(1+g)*Y2-(1+r)*P2)
#memory in profit function
if (memory) {
U1 <- filter((1-W)*Pi1, W, method="recursive")
U2 <- filter((1-W)*Pi2, W, method="recursive")
}
else {
U1 <- Pi1;
U2 <- Pi2;
}
dUi <- U1-U2
# normalize payoff differences
dUi <- dUi/(sd(dUi)+0.0000001)
# fractions of agents with rule 1, without asyncronous updating (general result)
N1 <- 1/(1+exp(-Beta*dUi))
# asynchronous updating
if (async.updating) {
N1 <-filter((1-Delta)*N1, Delta, method="recursive")
}
else {
N1 <- N1;
}
#predicted values
N2 <- 1- N1;
Q <- Psi*N1*Phi1+Psi*(N2)*Phi2
#return value of Pfit (this is pricing equation)
Y*Q
}
##########################################################################################
estimate.model <- function(P, Y, r, g){
library("nls2")
library('proto')
# Use nonlinear least squares to estimate model
nlmod =
nls(P~PfitHV(P, Y, r, g, Beta, Phi1, Phi2, Delta, W),
start = start.val,
lower = lower.val,
upper = upper.val,
algorithm = "port",
trace = FALSE,
control= nls.control(minFactor=1/10000, maxiter = 100))
#retreive estimated parameter values
d = summary(nlmod)
Phi1 = d$coefficients[1];
Phi2 = d$coefficients[2];
Beta = d$coefficients[3];
Delta = d$coefficients[4];
W = d$coefficients[5];
# redistil outcome variables using the fitted values
Pfit=PfitHV(P, Y, r, g, Beta, Phi1, Phi2, Delta, W);
res = P - Pfit;
nlmod # outcome of function
}
#################main program#############################################################
# read in data
data <- read.csv2('TRY2.csv', header=T, sep = ';', dec = ',', colClasses = "numeric", stringsAsFactors=FALSE)
newdata <- na.omit(data) #to omit NA from data
P <- newdata[,1] #imports prices
Y <- newdata[,2] #imports dividends
G <- newdata[,18] #imports growth rate real cash flows
Yield <- newdata[,17] #imports cash flow yield using dividends
# calculate r,g averages
Average_Yield <- mean(Yield)
g <- mean(G)/100
r <-g+Average_Yield/100
nlmod=0; #clear all
nlmod <- estimate.model(P, Y, r, g)
summary(nlmod)
如果需要,我也可以共享我的数据文件。