简体   繁体   English

df(X0)出错:缺少参数“df1”,没有默认值 - 跟踪R代码

[英]Error in df(X0) : argument “df1” is missing, with no default--tracing R code

I have written two gradient descent functions and in the second one I just have the alpha parameter and the initial alpha is different. 我写了两个梯度下降函数,在第二个中我只有alpha参数,初始alpha不同。 I receive a weird error and was unable to trace the reason for it. 我收到一个奇怪的错误,无法追查它的原因。

Here's the code: 这是代码:

k=19000
rho.prime<-function(t,k) ifelse (abs(t)<=k,2*t,(2*k*sign(t)))

dMMSE <- function(b,k=19000, y=farmland$farm, x=farmland$land){

  n = length(y)
  a=0
  d=0
  for (i in 1:n) {

    a = a + rho.prime(y[i]-b[1]-b[2]*x[i],k)
    d = d + x[i]*rho.prime(y[i]-b[1]-b[2]*x[i],k)
  }
  a <- (-a/n)
  d <- (-d/n)
  return(c(a,d))
}

grd=gr.descent(dMMSE, c(3500,0.33),alpha=0.0001, verbose=TRUE)

gr.descent2 <- function(dMMSE,x0, alpha=0.1, eps=0.001, max.it = 50, verbose = FALSE){
  X1 <- x0
  cond <- TRUE
  iteration <- 0
  if(verbose) cat("X0 =",X1,"\n")
  while(cond){
    iteration <- iteration + 1
    X0 <- X1
    X1 <- X0 - alpha * df(X0)
    alpha <- alpha/2
    cond <- sum((X1 - X0)^2) > eps & iteration < max.it
    if(verbose) cat(paste(sep="","X",iteration," ="), X1, "\n")
  }
  print("mona2")
  print(X1)
  return(X1)
}

grd2=gr.descent2(dMMSE, c(3500,0.33),alpha=0.1, verbose=TRUE)
#(beta0=grd2[1])
#(beta1=grd2[2])

So when I run the code I receive this error: 因此,当我运行代码时,我收到此错误:

[1] "mona"
[1]    3496.409 -259466.640
X0 = 3500 0.33 
 Show Traceback

 Rerun with Debug
 Error in df(X0) : argument "df1" is missing, with no default 

Which is related to gr.descent2 function. 这与gr.descent2功能有关。 Any thought? 任何想法?

Type this: 输入:

?df

And notice that the df1 and df2 arguments are not assumed to be any particular value so they do need to be supplied. 请注意,df1和df2参数不是任何特定值,因此需要提供它们。

integrate( function(x) df(x, 1, 100), 0, 3.84)
# 0.9471727 with absolute error < 1.4e-05

And notice the similarity of result: 并注意结果的相似性:

> integrate( function(x) dchisq(x, 1), 0, 3.84)
0.9499565 with absolute error < 1.4e-05

Here's the answer: 这是答案:

farmland <- read.csv("http://pages.stat.wisc.edu/~gvludwig/327-5/FarmLandArea.csv")
str(farmland)
plot(farm~land,data=farmland)
fit=lm(farm~land,data=farmland)
abline(fit) #lease square regression line
abline(rlm(farm~land,data=farmland),col="red")
gr.descent <- function(der_f, x0, alpha=0.0001, eps=0.001, max.it = 50, verbose = FALSE){
  X1 <- x0
  cond <- TRUE
  iteration <- 0
  if(verbose) cat("X0 =",X1,"\n")
  while(cond){
    iteration <- iteration + 1
    X0 <- X1
    X1 <- X0 - alpha * der_f(X0)
    cond <- sum((X1 - X0)^2) > eps & iteration < max.it
    if(verbose) cat(paste(sep="","X",iteration," ="), X1, "\n")
  }
  print("mona")
  print(X1)
  return(X1)
}



rho<-function(t,k) ifelse(abs(t)<=k,t^2,(2*k*abs(t))-k^2)
k=19000
rho.prime<-function(t,k) ifelse (abs(t)<=k,2*t,(2*k*sign(t)))

dMMSE <- function(b,k=19000, y=farmland$farm, x=farmland$land){

  n = length(y)
  a=0
  d=0
  for (i in 1:n) {

    a = a + rho.prime(y[i]-b[1]-b[2]*x[i],k)
    d = d + x[i]*rho.prime(y[i]-b[1]-b[2]*x[i],k)
  }
  a <- (-a/n)
  d <- (-d/n)
  return(c(a,d))
}

grd=gr.descent(dMMSE, c(3500,0.33),alpha=0.0001, verbose=TRUE)

gr.descent2 <- function(der_f,x0, alpha=0.1, eps=0.001, max.it = 50, verbose = FALSE){
  X1 <- x0
  cond <- TRUE
  iteration <- 0
  if(verbose) cat("X0 =",X1,"\n")
  while(cond){
    iteration <- iteration + 1
    X0 <- X1
    X1 <- X0 - alpha * der_f(X0)
    alpha <- alpha/2
    cond <- sum((X1 - X0)^2) > eps & iteration < max.it
    if(verbose) cat(paste(sep="","X",iteration," ="), X1, "\n")
  }
  print("mona2")
  print(X1)
  return(X1)
}

#plot(farm~land,data=farmland)
#curve(rho(k=19000),xlim=c(-10,10),,col="blue", add="TRUE")
grd2=gr.descent2(dMMSE, c(3500,0.33),alpha=0.1, verbose=TRUE)
#(beta0=grd2[1])
#(beta1=grd2[2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM