generate_data=function(T,theta.true){
epsilon=rnorm(T,0,theta.true[1])
y=epsilon
y[1]=epsilon[1]
for(t in 2:T)
{y[t]=theta.true[2]*epsilon[t-1]+epsilon[t]}
return(y)}
L=function(data,x1,x2){
dt=0
for(t in 1:T)
{dt=dt+log(x1*(1-x2^(2*t+2))/(1-x2^(2*t)))}
Y=rep(0,T)
Y[1]=data[1]
for(t in 2:T)
{Y[t]=data[t]-x2*(1-x2^(2*t-2))*Y[t-1]/(1-x2^(2*t))}
Sum=data[1]^2/((1+x2^2)*x1)
for(t in 2:T)
{Sum=Sum+Y[t]^2*(1-x2^(2*t))/(x1*(1-x2^(2*t+2)))}
l=-0.5*dt-0.5*Sum
return(l)
}
likeli=function(data,f,j){
deta=1e-5
g1=(L(f+deta,j)-L(f,j))/deta
g2=(L(f,j+deta)-L(f,j))/deta
g=c(g1,g2)
return(g)}
NR=function(data,epsilon){
A=diag(2)
theta.temp=c(0.2,0.6)
Gtemp=likeli(data,theta.temp[1],theta.temp[2])
for(iter in 1:T){
theta=theta.temp+A%*%Gtemp
G=likeli(data,theta[1],theta[2])
A=A-(A%*%(G-Gtemp)%*%t(G-Gtemp)%*%A)/(t(G-Gtemp)%*%A%*%(G-Gtemp))-((theta-theta.temp)%*%t(theta-theta.temp))/((t(G-Gtemp)%*%(theta-theta.temp))
dif=max(abs(theta-theta.temp))
if(dif<=epsilon)break
theta.temp=theta
Gtemp=likeli(data,theta.temp[1],theta.temp[2])}
return(theta)
}
近段时间在学时间序列分析,老师要求采取改进的牛顿迭代方法来进行极大似然估计
是对Ma(1)过程,但是问题出现在最后一个NR函数,
A=A-(A%*%(G-Gtemp)%*%t(G-Gtemp)%*%A)/(t(G-Gtemp)%*%A%*%(G-Gtemp))-((theta-theta.temp)%*%t(theta-theta.temp))/((t(G-Gtemp)%*%(theta-theta.temp))
dif=max(abs(theta-theta.temp))
之后会报错显示错误: unexpected symbol in:
"A=A-(A%*%(G-Gtemp)%*%t(G-Gtemp)%*%A)/(t(G-Gtemp)%*%A%*%(G-Gtemp))-((theta-theta.temp)%*%t(theta-theta.temp))/((t(G-Gtemp)%*%(theta-theta.temp))
dif"
A的表达式是按照书上的来的,检查了好多遍应该没有输错,但一直找不到错误原因,求各位大神帮帮忙!!感激不尽!!