以下是我正在使用的所有变量:
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) 我有以下代码用于使用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)
