1

I have created a package named test and I have a function named lad inside it. When I build it and after check it with cran=TRUE, I receive the following error. Any idea what's going wrong?

* checking examples ... ERROR
Running examples in 'test-Ex.R' failed
The error most likely occurred in:

> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: lad
> ### Title: LAD
> ### Aliases: lad
> 
> ### ** Examples
> 
> lad(y = "farm", x = "land", data="http://pages.stat.wisc.edu/~gvludwig/327-5/FarmLandArea.csv")
Error in (function (par)  : object 'sum.abs.dev' not found
Calls: lad -> optim -> <Anonymous>
Execution halted
Error: Command failed (1)

Here's the code inside power.R function which is in the R folder of my test package.

sum.abs.dev<-function(beta,a=land,b=farm)
{
  total<-0
  n<-length(b)
  for (i in 1:n)
  {
    total <- total + abs(b[i]-beta[1]-beta[2]*a[i])
  }
  return(total)
}

#' LAD
#' 
#' Minimize the sum of absolute deviations from the residuals
#' @param y A value showing the first column of the data frame
#' @param x A value showing the second column of the data frame
#' @param data A value showing the link to the data frame in CSV format
#' @return The square of the input
#' @export
#' @examples 
#' lad(y = "farm", x = "land", data="http://pages.stat.wisc.edu/~gvludwig/327-5/FarmLandArea.csv")

lad <- function(y = "farm", x = "land", data="http://pages.stat.wisc.edu/~gvludwig/327-5/FarmLandArea.csv")
{
  library(stats)
  dat <- read.csv(data)
  dat.x <- dat[[x]]
  dat.y <- dat[[y]]
  fit<-lm(dat.y~dat.x)
  beta.out=optim(fit$coefficients,sum.abs.dev)$par

  return(beta.out)
}

Here are the commands I ran so far before check:

build("/Users/mona/test")
build("/Users/mona/test", binary=TRUE)
check("/Users/mona/test", cran=FALSE)

When I click Build & Reload I don't receive any problem and here's what I receive:

> library(test)

Attaching package: ‘test’

The following object is masked _by_ ‘.GlobalEnv’:

    lad

enter image description here

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • you should create another piece of roxygen documentation like you did for sum.abs.dev – rawr May 01 '14 at 21:29
  • Thank you. But can you please take a look at my updated question? – Mona Jalal May 01 '14 at 21:49
  • I kept everything the same as you have, put `sum.abs.dev` inside of `lad`, right above the `beta.out`, and changed `sum.abs.dev<-function(beta,a = dat.x,b = dat.y)`. Does that work? – rawr May 01 '14 at 22:09
  • Is this [**homework**](http://pages.stat.wisc.edu/~gvludwig/327-5/)? Regarding asking people on SO to do your homework, please read [**this**](http://meta.stackoverflow.com/questions/10811/how-do-i-ask-and-answer-homework-questions/10812#10812). – Henrik May 02 '14 at 06:19
  • I know I should have had self-study tag but the other 4 tags beside R were really important! I wanted to delete but it didn't let me! So I don't know how to delete it! – Mona Jalal May 02 '14 at 06:22
  • If you had bothered to read the link in my comment, you would have found that (1) the "homework tag is deprecated", and (2) "**Be aware of school policy**. If your school has a policy regarding outside help on homework, make sure you are aware of it before you ask for / receive help on Stack Overflow.". So what is your school policy? [**"you must write programs by yourself"**](http://pages.stat.wisc.edu/~gvludwig/327-5/). And you keep posting homework questions...Good luck! – Henrik May 09 '14 at 22:50
  • 3
    I have asked my instructor and he has encouraged us highly to ask for help in stackoverflow when we are stuck. – Mona Jalal May 10 '14 at 05:27

1 Answers1

1

The following achieves what you want, I believe:

lad <- function(y, x, data) {
  dat <- setNames(read.csv(data)[, c(x, y)], c('x', 'y'))
  sum.abs.dev <- function(beta, data) {
    with(data, sum(abs(y - beta[1] - beta[2] * x)))
  }
  fit <- lm(y ~ x, dat)
  optim(par=coef(fit), sum.abs.dev, data=dat)$par
}

lad(y = "farm", x = "land", data="FarmLandArea.csv")

#  (Intercept)            x 
# -605.2293682    0.3642028 
jbaums
  • 27,115
  • 5
  • 79
  • 119
  • Thank you jbaums this gives the right answer for the output. However when I run the check with cran=TRUE I receive the error I have added in the updated post `Running examples in 'test-Ex.R' failed` do you know where I should add the file `test-Ex.R` ? Also what should I add there? just this : `lad(y = "farm", x = "land", data="http://pages.stat.wisc.edu/~gvludwig/327-5/FarmLandArea.csv")` ? thanks for the help. – Mona Jalal May 02 '14 at 02:32
  • What's is the example? – jbaums May 02 '14 at 03:22
  • on a side note how should I create a plot with the numbers given by the lad? Can you give me a hint? – Mona Jalal May 02 '14 at 03:25
  • I created a whole new project and now I have a new problem this time with DESCRIPTION file apparently. It'd be great if you check here: http://stackoverflow.com/questions/23420323/building-a-documented-package-in-r-check-fails-with-error-unexpected-symbol-in – Mona Jalal May 02 '14 at 03:39
  • 1
    Side note: depends what you want to plot. I imagine the points, along with the line described by the parameters estimated by `optim`? If so, try `dat <- read.csv('FarmlandArea.csv'); plot(dat$farm ~ dat$land); pars <- lad(y = "farm", x = "land", data="FarmLandArea.csv"); abline(pars[1], pars[2])`. – jbaums May 02 '14 at 03:45
  • The question is where in @example should I write these lines? I want to write these lines just when I run example(lad) not inside the lad function. – Mona Jalal May 02 '14 at 03:48
  • Should I write it inside jalal.Rd here ? `\examples{ pars<-lad(y = "farm", x = "land", data="http://pages.stat.wisc.edu/~gvludwig/327-5/FarmLandArea.csv"); dat <- read.csv(data); plot(dat$farm ~ dat$land); abline(pars[1], pars[2]) }` – Mona Jalal May 02 '14 at 03:51
  • The way you've written the example in roxygen looks fine to me. If you start a fresh workspace and manually load just the functions that are in your package, does the example work? – jbaums May 02 '14 at 04:39
  • No, every time I run it, it overwrites whatever I write in the .Rd file. – Mona Jalal May 02 '14 at 04:42