Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels

爷,独闯天下 提交于 2019-12-04 06:23:19

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!