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