Skip to main content
edited tags
Link
Tim
  • 141.2k
  • 26
  • 270
  • 512
edited body; edited title
Source Link
kjetil b halvorsen
  • 82.8k
  • 32
  • 201
  • 663

confusion Confusion about cv.glm in R

R's document says that delta is the raw cross-validation estimate of prediction error, which i think is prediction error rate in the situation of logistic regression. However, when i try to calculate prediction error rate with my own function the result is different.

cv.glm:

> fit=glm(Direction~Lag1+Lag2,family = binomial,data = Weekly)
> cv.err=cv.glm(Weekly,fit)
> cv.err$delta[1]
[1] 0.2464536

my function:

> fun=function(){
+     count=0
+     for(i in 1:length(Direction)){
+         fit=glm(Direction~Lag1+Lag2,family = binomial,data = Weekly[-i,])
+         prob=predict(fit,newdata = Weekly[i,],type = "response")
+         pred="Down"
+         if(prob>0.5)
+             pred="Up"
+         if(pred!=Direction[i])
+             count=count+1
+     }
+     return(count/length(Direction))
+ }
> fun()
[1] 0.4499541

whyWhy the result is different? Could anyone explain this for me?

confusion about cv.glm in R

R's document says that delta is the raw cross-validation estimate of prediction error, which i think is prediction error rate in the situation of logistic regression. However, when i try to calculate prediction error rate with my own function the result is different.

cv.glm:

> fit=glm(Direction~Lag1+Lag2,family = binomial,data = Weekly)
> cv.err=cv.glm(Weekly,fit)
> cv.err$delta[1]
[1] 0.2464536

my function:

> fun=function(){
+     count=0
+     for(i in 1:length(Direction)){
+         fit=glm(Direction~Lag1+Lag2,family = binomial,data = Weekly[-i,])
+         prob=predict(fit,newdata = Weekly[i,],type = "response")
+         pred="Down"
+         if(prob>0.5)
+             pred="Up"
+         if(pred!=Direction[i])
+             count=count+1
+     }
+     return(count/length(Direction))
+ }
> fun()
[1] 0.4499541

why the result is different? Could anyone explain this for me?

Confusion about cv.glm in R

R's document says that delta is the raw cross-validation estimate of prediction error, which i think is prediction error rate in the situation of logistic regression. However, when i try to calculate prediction error rate with my own function the result is different.

cv.glm:

> fit=glm(Direction~Lag1+Lag2,family = binomial,data = Weekly)
> cv.err=cv.glm(Weekly,fit)
> cv.err$delta[1]
[1] 0.2464536

my function:

> fun=function(){
+     count=0
+     for(i in 1:length(Direction)){
+         fit=glm(Direction~Lag1+Lag2,family = binomial,data = Weekly[-i,])
+         prob=predict(fit,newdata = Weekly[i,],type = "response")
+         pred="Down"
+         if(prob>0.5)
+             pred="Up"
+         if(pred!=Direction[i])
+             count=count+1
+     }
+     return(count/length(Direction))
+ }
> fun()
[1] 0.4499541

Why the result is different? Could anyone explain this for me?

Post Reopened by Tim, whuber
Post Closed as "Not suitable for this site" by whuber
added 40 characters in body
Source Link
Bob
  • 63
  • 1
  • 1
  • 6

R's document says that delta is the raw cross-validation estimate of prediction error, which i think is prediction error rate in the situation of logistic regression. However, when i try to calculate prediction error rate with my own function the result is different.

cv.glm:

> fit=glm(Direction~Lag1+Lag2,family = binomial,data = Weekly)
> cv.err=cv.glm(Weekly,fit)
> cv.err$delta[1]
[1] 0.2464536

my function:

> fun=function(){
+     count=0
+     for(i in 1:length(Direction)){
+         fit=glm(Direction~Lag1+Lag2,family = binomial,data = Weekly[-i,])
+         prob=predict(fit,newdata = Weekly[i,],type = "response")
+         pred="Down"
+         if(prob>0.5)
+             pred="Up"
+         if(pred!=Direction[i])
+             count=count+1
+     }
+     return(count/length(Direction))
+ }
> fun()
[1] 0.4499541

why the result is different? Could anyone explain this for me?

R's document says that delta is the raw cross-validation estimate of prediction error, which i think is prediction error rate. However, when i try to calculate prediction error rate with my own function the result is different.

cv.glm:

> fit=glm(Direction~Lag1+Lag2,family = binomial,data = Weekly)
> cv.err=cv.glm(Weekly,fit)
> cv.err$delta[1]
[1] 0.2464536

my function:

> fun=function(){
+     count=0
+     for(i in 1:length(Direction)){
+         fit=glm(Direction~Lag1+Lag2,family = binomial,data = Weekly[-i,])
+         prob=predict(fit,newdata = Weekly[i,],type = "response")
+         pred="Down"
+         if(prob>0.5)
+             pred="Up"
+         if(pred!=Direction[i])
+             count=count+1
+     }
+     return(count/length(Direction))
+ }
> fun()
[1] 0.4499541

why the result is different? Could anyone explain this for me?

R's document says that delta is the raw cross-validation estimate of prediction error, which i think is prediction error rate in the situation of logistic regression. However, when i try to calculate prediction error rate with my own function the result is different.

cv.glm:

> fit=glm(Direction~Lag1+Lag2,family = binomial,data = Weekly)
> cv.err=cv.glm(Weekly,fit)
> cv.err$delta[1]
[1] 0.2464536

my function:

> fun=function(){
+     count=0
+     for(i in 1:length(Direction)){
+         fit=glm(Direction~Lag1+Lag2,family = binomial,data = Weekly[-i,])
+         prob=predict(fit,newdata = Weekly[i,],type = "response")
+         pred="Down"
+         if(prob>0.5)
+             pred="Up"
+         if(pred!=Direction[i])
+             count=count+1
+     }
+     return(count/length(Direction))
+ }
> fun()
[1] 0.4499541

why the result is different? Could anyone explain this for me?

Source Link
Bob
  • 63
  • 1
  • 1
  • 6
Loading