相关疑难解决方法(0)

如何调试"对比只能应用于具有2级或更多级别的因素"错误?

以下是我正在使用的所有变量:

str(ad.train)
$ Date                : Factor w/ 427 levels "2012-03-24","2012-03-29",..: 4 7 12 14 19 21 24 29 31 34 ...
 $ Team                : Factor w/ 18 levels "Adelaide","Brisbane Lions",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Season              : int  2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 ...
 $ Round               : Factor w/ 28 levels "EF","GF","PF",..: 5 16 21 22 23 24 25 26 27 6 ...
 $ Score               : int  137 …
Run Code Online (Sandbox Code Playgroud)

regression r lm glm r-faq

23
推荐指数
1
解决办法
5万
查看次数

`对比<-`(`*tmp*`,value = contr.funs [1 + isOF [nn]])出错:对比只能应用于2级或更多级别的因子

我有以下代码用于使用optim()来查找beta0和beta1来最小化偏差之和但我收到以下错误我不知道我做错了什么:

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)
}
Run Code Online (Sandbox Code Playgroud)

这是错误和警告:

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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

csv optimization regression r function

6
推荐指数
1
解决办法
4万
查看次数

标签 统计

r ×2

regression ×2

csv ×1

function ×1

glm ×1

lm ×1

optimization ×1

r-faq ×1