[R] Error of Cross Validation

jim holtman jholtman at gmail.com
Mon Jun 20 14:40:07 CEST 2011


You real error was this:

Error in pre[sam[[j]], i] <- predict(fit, data = dat[sam[[j]], ]) :
  number of items to replace is not a multiple of replacement length

Take a look at the statement and you will see that 'predict' is
returning 100 values and you are trying to store it into only 10 rows
(that is what sam[[j]] is).  You need to rethink what you are trying
to do.  Let me ask my favorite question:  what is the problem you are
trying to solve?

On Mon, Jun 20, 2011 at 8:05 AM, zhu yao <mailzhuyao at gmail.com> wrote:
> error in pre[sam[[j]], i] <- predict(fit, data = dat[sam[[j]], ]) :
>
> Yao Zhu
> Department of Urology
> Fudan University Shanghai Cancer Center
> Shanghai, China
>
>
> 2011/6/20 Jim Holtman <jholtman at gmail.com>
>>
>> what was the error?
>>
>> Sent from my iPad
>>
>> On Jun 20, 2011, at 1:34, zhu yao <mailzhuyao at gmail.com> wrote:
>>
>> > Dear R users:
>> >
>> > Recently, I tried to write a program to calculate cross-validated
>> > predicted
>> > value.
>> > My sources are as follows. However, the R reported an error.
>> > Could you please check the sources? Thanks.
>> >
>> > set.seed(100)
>> > x<-rnorm(100)
>> > y<-sample(rep(0:1,50),replace=T)
>> > dat<-data.frame(x,y)
>> >
>> > library(rms)
>> >
>> > fito<-lrm(y~x)
>> > preo<-predict(fito)
>> >
>> > pre<-matrix(NA,nrow=100,ncol=200)
>> >
>> > for (i in 1:200)
>> > {
>> > sam<-sample(1:nrow(dat))
>> > sam<-split(sam,1:10)
>> > for (j in 1:10)
>> > {
>> > fit<-lrm(y~x,data=dat[-sam[[j]],])
>> > pre[sam[[j]],i]<-predict(fit,data=dat[sam[[j]],])
>> > }
>> > }
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> > *Yao Zhu*
>> > *Department of Urology
>> > Fudan University Shanghai Cancer Center
>> > Shanghai, China*
>> >
>> >    [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list