Help needed

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Help needed

Anamika Chaudhuri
Hi,
   
  I am trying to change a SAS macro to R.
   
  here is my code. I get an error at the last line.
  attach(fram)
  dset1<-cbind(AGE,BMI,DEATH)

> BMIGRP<-cut(BMI,breaks=3,right=TRUE)
> AGEGRP<-floor(AGE/10)-2
> dset<-cbind(AGEGRP,BMIGRP,DEATH)
> maxage<-max(dset[,1])
> minage<-min(dset[,1])
> #maxcls<-dset[,2]
> #mincls<-dset[,2]
> nage<-maxage-minage+1
> nclass<-maxcls-mincls+1
> nsub<-nrow(dset)
> weight <- matrix(,nage,1)
> for ( i in minage:maxage )
+   {weight[i-minage+1,1] = sum(std = i)}
>
>   atrisk = matrix(,nclass,nage)
>   wevents = matrix(,nclass,nage)
> #reduce data set to frequency table
>  for( i in (minage : maxage))
+  for( j in (mincls : maxcls))
+ #atrisk1<-aggregate(dset[,c("AGEGRP","BMIGRP")],list(RANDID=dset1$RANDID,sum)
+
+     {atrisk[j-mincls+1,i-minage+1] =
+      sum((dset[,1]=i)& (dset[,2]=j))
+     wevents[j-mincls+1,i-minage+1]=
+       sum((dset[,1]=i)& (dset[,2]=j)&(dset[,3]=1))}
Error: subscript out of bounds
In addition: Warning messages:
1: numerical expression has 11627 elements: only the first used in: mincls:maxcls
2: numerical expression has 11627 elements: only the first used in: mincls:maxcls
   
  Any help will be greatly appreciated.
   
  Thanks,
  Anamika

               
---------------------------------

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Help needed

Richard M. Heiberger
The lines
> #maxcls<-dset[,2]
> #mincls<-dset[,2]
which you have shown commented out select a full column.
You probably want the min and max of that column.

With your definitions, mincls:maxlcs has the same type of behavior as
(1:3):(1:3)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Help needed

Anamika Chaudhuri
Hi,
   
  Thanks but I had already fixed that part.
  My problem is I am not getting a value for the maxcls or mincls:
  here is the code:
   
  > dset1<-cbind(AGE,BMI,DEATH)
> BMIGRP<-cut(BMI,breaks=c(14,20,25,57),right=TRUE)
> AGEGRP<-floor(AGE/10)-2
> dset<-cbind(AGEGRP,BMIGRP,DEATH)
> maxage<-max(dset[,1])
> minage<-min(dset[,1])
> maxcls<-max(dset[,2])
> maxcls
[1] NA
   
  After performing the cut function i get the different categories which are labelled as (a,b] by default. I wanted to change them to categories 1 ,2 and 3 respectively so that I get a value for maxcls and mincls.
   
  How do I change the 3 categories (14,20],(20,25],(25,57] to 1,2,3 respectively?
   
  Thanks,
  Anamika
 

"Richard M. Heiberger" <[hidden email]> wrote:
  The lines
> #maxcls<-dset[,2]
> #mincls<-dset[,2]
which you have shown commented out select a full column.
You probably want the min and max of that column.

With your definitions, mincls:maxlcs has the same type of behavior as
(1:3):(1:3)


               
---------------------------------

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Help needed

Richard M. Heiberger
In reply to this post by Anamika Chaudhuri
> x <- rnorm(100)
> xx <- cut(x,3)
> levels(xx)
[1] "(-2.37,-0.716]" "(-0.716,0.933]" "(0.933,2.58]"  
> as.numeric(xx)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Help needed

Anamika Chaudhuri
In reply to this post by Anamika Chaudhuri
As my earlier email said I am not getting the maxcls. I do get numbers coded as 1,2,3 for BMIGRP(when I print BMIGRP) but not getting the max of (1,2,3) which should be 3, I guess.
   
  Thanks for your help
  Anamika
 

Anamika Chaudhuri <[hidden email]> wrote:
    > dset1<-cbind(AGE,BMI,DEATH)
> BMIGRP<-cut(BMI,breaks=c(14,20,25,57),right=TRUE)
> levels(BMIGRP)
[1] "(14,20]" "(20,25]" "(25,57]"
> BMIGRP1<-as.numeric(BMIGRP)
> AGEGRP<-floor(AGE/10)-2
> dset<-cbind(AGEGRP,BMIGRP1,DEATH)
> maxage<-max(dset[,1])
> minage<-min(dset[,1])
> maxcls<-max(dset[,2])
> maxcls
[1] NA
   
  Why doesnt it give me a no for maxcls then?
   
  Thanks.
 

"Richard M. Heiberger" <[hidden email]> wrote:
  > x <- rnorm(100)
> xx <- cut(x,3)
> levels(xx)
[1] "(-2.37,-0.716]" "(-0.716,0.933]" "(0.933,2.58]"
> as.numeric(xx)

   
---------------------------------


               
---------------------------------

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Help needed

PIKAL Petr
Hi

you experienced difference between matrix (cbind(....)) and data
frame (data.frame(......)) or you have some NA value in your BMIGRP1

try:
maxcls<-max(dset[,2], na.rm=T)
if you have some NA values
or use
str(dset) to look what types are your variables.

HTH
Petr


On 25 Apr 2006 at 18:16, Anamika Chaudhuri wrote:

Date sent:       Tue, 25 Apr 2006 18:16:42 -0700 (PDT)
From:           Anamika Chaudhuri <[hidden email]>
To:             "Richard M. Heiberger" <[hidden email]>, [hidden email]
Subject:         Re: [R] Help needed

> As my earlier email said I am not getting the maxcls. I do get numbers
> coded as 1,2,3 for BMIGRP(when I print BMIGRP) but not getting the max
> of (1,2,3) which should be 3, I guess.
>
>   Thanks for your help
>   Anamika
>
>
> Anamika Chaudhuri <[hidden email]> wrote:
>     > dset1<-cbind(AGE,BMI,DEATH)
> > BMIGRP<-cut(BMI,breaks=c(14,20,25,57),right=TRUE)
> > levels(BMIGRP)
> [1] "(14,20]" "(20,25]" "(25,57]"
> > BMIGRP1<-as.numeric(BMIGRP)
> > AGEGRP<-floor(AGE/10)-2
> > dset<-cbind(AGEGRP,BMIGRP1,DEATH)
> > maxage<-max(dset[,1])
> > minage<-min(dset[,1])
> > maxcls<-max(dset[,2])
> > maxcls
> [1] NA
>
>   Why doesnt it give me a no for maxcls then?
>
>   Thanks.
>
>
> "Richard M. Heiberger" <[hidden email]> wrote:
>   > x <- rnorm(100)
> > xx <- cut(x,3)
> > levels(xx)
> [1] "(-2.37,-0.716]" "(-0.716,0.933]" "(0.933,2.58]"
> > as.numeric(xx)
>
>
> ---------------------------------
>
>
>
> ---------------------------------
>
>  [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html