> n <- 10; # Sample size
>
> fitglm <- function(sigma,tau){
+ x <- rnorm(n,0,sigma)
+ intercept <- 0
+ beta <- 0
+ ystar <- intercept+beta*x
+ z <- rbinom(n,1,plogis(ystar))
+ xerr <- x + rnorm(n,0,tau)
+ model<-glm(z ~ xerr, family=binomial(logit))
+ int<-coef(model)[1]
+ slope<-coef(model)[2]
+ pred<-predict(model)
+
+ result<-ifelse(pred>.5,1,0)
+
+ accuracy<-length(which(result==z))/length(z)
+ accuracy
+
+ rocpreds<-prediction(result,z)
+ auc<-performance(rocpreds,"auc")@y.values
+ fp<-performance(rocpreds,"sens")
+ sentiv<-slot(fp,"y.values")[[1]]
+ sentiv<-sentiv[2]
+ sentiv
+ fp2<-performance(rocpreds,"spec")
+ specs<-slot(fp2,"y.values")[[1]]
+ specs<-specs[2]
+ specs
+ output<-c(int,slope,.5,accuracy,auc,sentiv,specs)
+
names(output)<-c("Intercept","Slope","CutPoint","Accuracy","AUC","Sentivity","Specificity")
+ return(output)
+
+ }
>
> y<-fitglm(2,1)
> y
$Intercept
[1] 1.335284

$Slope
[1] 0.1562984

$CutPoint
[1] 0.5

$Accuracy
[1] 0.8

$AUC
[1] 0.5

$Sentivity
[1] 1

$Specificity
[1] 0

>

Don't get error message but wrong values

On Thu, Oct 25, 2012 at 4:05 PM, Berend Hasselman <bhh@xs4all.nl> wrote:

>
>
> Your code is still not runnable.
> It gives the error message
>
> Error in fitglm(0.05, 1) : could not find function "prediction"
>
> Berend
>
> On 25-10-2012, at 21:55, Adel Powell wrote:
>
> > I think I have corrected it. Can you tell me are my spec and sens values
> correct
> > n <- 1000; # Sample size
> >
> > fitglm <- function(sigma,tau){
> >     x <- rnorm(n,0,sigma)
> >     intercept <- 0
> >     beta <- 0
> >     ystar <- intercept+beta*x
> >     z <- rbinom(n,1,plogis(ystar))
> >     xerr <- x + rnorm(n,0,tau)
> >     model<-glm(z ~ xerr, family=binomial(logit))
> >     int<-coef(model)[1]
> >     slope<-coef(model)[2]
> >     pred<-predict(model)
> >
> >     result<-ifelse(pred>.5,1,0)
> >
> >     accuracy<-length(which(result==z))/length(z)
> >     accuracy
> >
> >     rocpreds<-prediction(result,z)
> >     auc<-performance(rocpreds,"auc")@y.values
> >     fp<-performance(rocpreds,"sens")
> >     sentiv<-slot(fp,"y.values")[[1]]
> >     sentiv<-sentiv[2]
> >     sentiv
> >     fp2<-performance(rocpreds,"spec")
> >     specs<-slot(fp2,"y.values")[[1]]
> >     specs<-specs[2]
> >     specs
> >     output<-c(int,slope,.5,accuracy,auc,sentiv,specs)
> >
> names(output)<-c("Intercept","Slope","CutPoint","Accuracy","AUC","Sentivity","Specificity")
> >     return(output)
> >
> > }
> >
> > y<-fitglm(.05,1)
> > y
> >
> >
> > On Thu, Oct 25, 2012 at 3:43 PM, Berend Hasselman <bhh@xs4all.nl> wrote:
> >
> > On 25-10-2012, at 21:28, Adel Powell wrote:
> >
> > > I am running my code in a loop and it does not work but when I run it
> > > outside the loop I get the values I want.
> > >
> > > n <- 1000; # Sample size
> > >
> > > fitglm <- function(sigma,tau){
> > >    x <- rnorm(n,0,sigma)
> > >    intercept <- 0
> > >    beta <- 0
> > >    ystar <- intercept+beta*x
> > >    z <- rbinom(n,1,plogis(ystar))
> > >    xerr <- x + rnorm(n,0,tau)
> > >    model<-glm(z ~ xerr, family=binomial(logit))
> > >    int<-coef(model)[1]
> > >    slope<-coef(model)[2]
> > >    pred<-predict(model)
> > >
> > >    result<-ifelse(pred>.5,1,0)
> > >
> > >    accuracy<-length(which(result==z))/length(z)
> > >    accuracy
> > >
> > >    rocpreds<-prediction(result,z)
> > >    auc<-performance(rocpreds,"auc")@y.values
> > >    sentiv<-performance(rocpreds,"sens")@y.values
> > >    sentiv<-slot(fp,"y.values")[[1]]
> > >    sentiv<-sentiv[2]
> > >    sentiv
> > >    specs<-performance(rocpreds,"spec")@y.values
> > >    specs<-slot(fp2,"y.values")[[1]]
> > >    specs<-specs[2]
> > >    specs
> > >    output<-c(int,slope,.5,accuracy,auc,sentiv,specs)
> > >
> > >
> names(output)<-c("Intercept","Slope","CutPoint","Accuracy","AUC","Sentivity",Specificity")
> >
> > A missing " before Specificity?
> >
> > >    return(output)
> > >
> > > }
> > >
> > > y<-fitglm(.05,1)
> > > y
> > >
> >
> > Running this after correction of the missing "  one gets en error
> >
> > Error in fitglm(0.05, 1) : could not find function "prediction"
> >
> > How are you using a loop?
> > Your example is not reproducible.
> >
> > Berend
> >
> >
> > >
> > > The code runs without the sentiv and specs but when I remove the loop
> i can
> > > get the sensitivity and spec. values ???
> > >
> > >       [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help@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.
> >
> >
>
>

	[[alternative HTML version deleted]]


