[R] The question is on Symmetry model for square table.

Spencer Graves spencer.graves at pdf.com
Thu Jul 10 17:08:17 CEST 2003


	  Did you try "traceback()"?  This might help you identify the 
offending line in your function.

	  If that doesn't help, I step through the function one line at a time 
(copy and paste from an editor) until R bombs on me.  If it doesn't 
bomb, then there is a "scoping" problem:  Are you using global variables 
in a function?  If yes, pass them explicitly as arguments.  I recently 
potentially similar problems this way.

hope this helps.  spencer graves

Adejumo Adebowale Olusola wrote:
> Please help,
> I tried a program on S-plus, and it worked. Also I tried the same 
> program on R but not worked. Here is the programme. I put it in a 
> function form. The model and assumption are at the bottom.
> 
> where
> counts<-c(22,2,2,0,5,7,14,0,0,2,36,0,0,1,17,10)
> which is name.data, i is row size and j is the column size.
> 
> symmetry
> function(i, j, name.data)
> {
>     row <- (c(1:i))
>     col <- (c(1:j))
>     name.data <- expand.grid(A = row, B = col)
>     name.data$counts <- c(counts)
>     name.data$symm <- paste(pmin(as.numeric(name.data$A),            
> as.numeric(name.data$B)), pmax(as.numeric(name.data$A),          
> as.numeric(name.data$B)), sep = ",")
>     symmetry <- glm(counts ~ symm, family = poisson(link = 
> log),             data = name.data)
> }
>  > summary(symmetry(4,4,counts))
> 
> Call: glm(formula = counts ~ symm, family = poisson(link = log), data = 
> name.data)
> Deviance Residuals:
>        Min         1Q         Median        3Q      Max
>  -4.123106 -0.9044956 -3.846197e-008 0.6543513 2.562617
> 
> Coefficients:
>                  Value Std. Error    t value
> (Intercept)  0.7912419  2.1128915  0.3744830
>       symm1 -0.9191397  0.2169745 -4.2361653
>       symm2 -0.7239676  0.2465491 -2.9364038
>       symm3 -2.3094242  5.2703608 -0.4381909
>       symm4  0.5614798  1.0575027  0.5309488
>       symm5  0.3965751  0.7045443  0.5628817
>       symm6 -0.1128162  0.5223163 -0.2159920
>       symm7  0.4499711  0.3777980  1.1910362
>       symm8  0.1895939  0.2946399  0.6434767
>       symm9  0.1679270  0.2368599  0.7089720
> 
> (Dispersion Parameter for Poisson family taken to be 1 )
> 
>     Null Deviance: 190.398 on 15 degrees of freedom
> 
> Residual Deviance: 39.17989 on 6 degrees of freedom
> 
> Number of Fisher Scoring Iterations: 6
> 
> _________________________________________________________________________
> Also in R. program, here is the same program together with the complain.
> 
> name.data=counts (above).
> 
>  > symmetry
> function(i,j,name.data){
> A<-(c(1:i))
> B<-(c(1:j))
> name.data<-expand.grid(A=A,B=B)
> name.data$counts<-(c(counts))
> name.data$symm<-paste(pmin(as.numeric(name.data$A),as.numeric(name.data$B)), 
> 
> pmax(as.numeric(name.data$A),as.numeric(name.data$B)),sep=",")
> symmetry<-glm(counts~symm,data=name.data,family=poisson(link=log))
> }
> 
>  > symmetry(4,4,counts)
> Error in model.frame(formula, rownames, variables, varnames, extras, 
> extranames,  : invalid variable type
> 
> I tried to print out the table with symm pathern. and the function for 
> symm below.
> 
>  > i<-4
>  > j<-4
>  > A<-(c(1:i))
>  > B<-(c(1:j))
>  > name.data<-expand.grid(A=A,B=B)
>  > name.data$counts<-(c(counts))
>  >name.data$symm<-paste(pmin(as.numeric(name.data$A),as.numeric(name.data$B)),
> + pmax(as.numeric(name.data$A),as.numeric(name.data$B)),sep=",")
>  > name.data
>    A B counts symm
> 1  1 1     22  1,1
> 2  2 1      2  1,2
> 3  3 1      2  1,3
> 4  4 1      0  1,4
> 5  1 2      5  1,2
> 6  2 2      7  2,2
> 7  3 2     14  2,3
> 8  4 2      0  2,4
> 9  1 3      0  1,3
> 10 2 3      2  2,3
> 11 3 3     36  3,3
> 12 4 3      0  3,4
> 13 1 4      0  1,4
> 14 2 4      1  2,4
> 15 3 4     17  3,4
> 16 4 4     10  4,4
>  >
> 
>  > symm
> function (x, levels = sort(unique.default(x), na.last = TRUE),
>     labels = levels, exclude = NA, ordered = is.ordered(x))
> {
>     if (is.null(x))
>         x <- list()
>     exclude <- as.vector(exclude, typeof(x))
>     levels <- levels[is.na(match(levels, exclude))]
>     f <- match(x, levels)
>     names(f) <- names(x)
>     nl <- length(labels)
>     attr(f, "levels") <- if (nl == length(levels))
>         as.character(labels)
>     else if (nl == 1)
>         paste(labels, seq(along = levels), sep = "")
>     else stop(paste("invalid labels; length", nl, "should be 1 or",
>         length(levels)))
>     class(f) <- c(if (ordered) "ordered", "factor")
>     f
> }
> 
> ----------------------------------------------------------------------------- 
> 
> The model and Assumptions
> 
> log(m_ij)= lambda + lambda_i + lambda_j + lambda_ij
> 
> where,
> lambda_ij = lambda_ji for i not equal to j
> and lambda_i(A) = lambda_i(B)
> Likelihood equation is
> 
> m_ij =(n_ij + n_ji)/2
> 
> For symmetry m_(ij)=m_(ji)
> 
> "R program" does not recognised "symm" pathern, that is (1,1), (1,2) and 
> so on but "S-plus" do. So please I need your assistance.
> 
> Thanks for your usual contibutions.
> 
> Yours
> 
> Sola.
> 
>




More information about the R-help mailing list