[Rd] Subscripting Matrices

2014-08-06 Thread Terrence Ireland
There seems to be a result type difference when subscripting a 6 x 1 
matrix as compared to a 3 x 2 matrix that is caused by the ncol = 1 
compared to ncol > 1.


> ThinMatrix <- matrix(1:6,ncol=1)
> ThinMatrix
 [,1]
[1,]1
[2,]2
[3,]3
[4,]4
[5,]5
[6,]6
> FatMatrix <- matrix(1:6,ncol=2)
> FatMatrix
 [,1] [,2]
[1,]14
[2,]25
[3,]36
> dim(ThinMatrix[TRUE,])
NULL  #Though this value should be 6 1
> dim(FatMatrix[TRUE,])
[1] 3 2

Thanks for your help.

Terry Ireland

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Scope question concerning calls within a user defined function

2009-05-06 Thread Terrence Ireland

The following is a simple example with a poor solution that shows my
difficulties with scope.  The function /logit.test /has 3 arguments:  
/model.start,/
an initial model; /model.finish/, an all-inclusive model, /my.data/, a 
dataset, in
this case trivial. 

There are 2 function calls in l/ogit.test,/ first to /glm/ to get an 
initial fit (local variable
/logit/) using /model.start;/ then a call to /stepAIC/ using the initial 
fit.


I had thought that l/ogit/ would carry along the dataset, /test.data/ 
for use in
the call to /stepAIC/.  Instead it complains that it cannot find 
/my.data/.  It seems
to have found the name, not the value in the /call/ attribute. 

I fixed the problem by creating a global variable /my.data/ within the 
function,

naming it l/ogit.test.global,/ not a very good solution.

My question is:  How do I handle scope correctly?

Thanks,

Terry Ireland

Script of Experiment:

> library(MASS)

> test.data
 Approve Score OldScore
1 Yes 1   12
2 Yes 3   10
3 Yes 58
4  No 49
5  No 67
6  No 85
> test.start
Approve ~ 1
> test.finish
Approve ~ Score + OldScore
> logit.test
function(model.start,model.finish,my.data)
 {
   logit <- glm(model.start,binomial(link = 
"logit"),my.data,control=glm.control(epsilon=0.0001),x=TRUE,y=TRUE);

   Table <- stepAIC(logit,scope=model.finish,direction="both",trace=1,k=2);
 }
> logit.test.global
function(model.start,model.finish,my.data)
 {
   my.data <<- my.data
   logit <- glm(model.start,binomial(link = 
"logit"),my.data,control=glm.control(epsilon=0.0001),x=TRUE,y=TRUE);

   Table <- stepAIC(logit,scope=model.finish,direction="both",trace=1,k=2);
 }

> logit.test(test.start,test.finish,test.data)
Start:  AIC=10.32
Approve ~ 1

Error in inherits(x, "data.frame") : object "my.data" not found
> logit.test.global(test.start,test.finish,test.data)
Start:  AIC=10.32
Approve ~ 1

  Df Deviance AIC
+ Score 1   4.8089  8.8089
+ OldScore  1   4.8089  8.8089
  8.3178 10.3178

Step:  AIC=8.81
Approve ~ Score

   Df Deviance AIC
   4.8089  8.8089
- Score  1   8.3178 10.3178
>

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel