6

I have the following code for minimizing the sum of deviation using optim() to find beta0 and beta1 but I am receiving the following errors I am not sure what I am doing wrong:

sum.abs.dev<-function(beta=c(beta0,beta1),a,b)
{
  total<-0
  n<-length(b)
  for (i in 1:n)
  {
    total <- total + (b[i]-beta[1]-beta[2]*a[i])
  }
  return(total)
}
tlad <- function(y = "farm", x = "land", data="FarmLandArea.csv")
{

  dat <- read.csv(data)

  #fit<-lm(dat$farm~dat$land)
  fit<-lm(y~x,data=dat)
  beta.out=optim(fit$coefficients,sum.abs.dev)

  return(beta.out)
}

Here's the error and warnings are receive:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels In addition: Warning message:
In model.response(mf, "numeric") : NAs introduced by coercion

enter image description here

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • Remove the default arguments in `tlad` and then specify the variables without quotation marks when you call the function. You also have `dat` instead of `data` specified in your `lm` call. – Thomas May 01 '14 at 19:13
  • you mean changing it to `tlad <- function(y = farm, x = land, data="FarmLandArea.csv") { dat <- read.csv(data) #fit<-lm(dat$farm~dat$land) fit<-lm(y~x,data=dat) beta.out=optim(fit$coefficients,sum.abs.dev) return(beta.out) }` ? – Mona Jalal May 01 '14 at 19:14
  • with that I am receiving this? `> tlad(farm,land,data="FarmLandArea.csv") Show Traceback Rerun with Debug Error in eval(expr, envir, enclos) : object 'farm' not found ` – Mona Jalal May 01 '14 at 19:14
  • See my edited comment. – Thomas May 01 '14 at 19:15
  • Sorry can you provide it with more detail. I can't get it – Mona Jalal May 01 '14 at 19:24
  • 3
    if this is homework, please tag it as such ... – Ben Bolker May 01 '14 at 21:17
  • This will clear your issue [Error in contrasts when defining a linear model in R](http://stackoverflow.com/questions/18171246/error-in-contrasts-when-defining-a-linear-model-in-r) – parth Aug 31 '16 at 15:28

1 Answers1

5

There are several problems here:

  1. You are specifying variables as character strings, so this line (fit<-lm(y~x,data=dat)) is interpreted by R as fit<-lm("farm"~"land",data=dat).
  2. It is easier to not specify default variables in your function because of scoping issues.

I would consider the following instead:

tlad <- function(y, x){      
  fit <- lm(y~x)
  beta.out <- optim(fit$coefficients, sum.abs.dev)
  return(beta.out)
}

dat <- read.csv("FarmLandArea.csv")
tlad(dat$farm, dat$land)
Thomas
  • 43,637
  • 12
  • 109
  • 140