df(X0)中的错误:参数“df1”丢失,没有默认 - 跟踪R代码 [英] Error in df(X0) : argument "df1" is missing, with no default--tracing R code

查看:292
本文介绍了df(X0)中的错误:参数“df1”丢失,没有默认 - 跟踪R代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了两个梯度下降函数,而在第二个中,我只有alpha参数,并且初始alpha是不同的。我收到一个奇怪的错误,无法追查它的原因。

I have written two gradient descent functions and in the second one I just have the alpha parameter and the initial alpha is different. 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 

这与gr.descent2函数有关。任何想法?

Which is related to gr.descent2 function. Any thought?

推荐答案

以下是答案:

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])

这篇关于df(X0)中的错误:参数“df1”丢失,没有默认 - 跟踪R代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆