-1

I have a ggm4 object produced by (for example) the following:

fit<-gamm4(formula=mass~s(hemato)+s(frass)+s(protos),random=~(1|nest)+(1|year),data=dat2)

If I extract the random formula from this object by specifying:

formula(fit$mer,random=T)

then it produces the following, which is also a formula object:

~(1 | Xr) + (1 | Xr.0) + (1 | Xr.1) + (1 | nest) + (1 | year)

I only want to extract the original random function that was specified in the call to gamm4 (i.e. ~(1|nest)+(1|year) while ignoring the parts of this formula that refer to to smoothing terms (i.e. (1 | Xr) etc.).

Context: This will be done repeatedly from inside another function in which fixed and random formulae will change.

Question: How can I do this?

I tried:

x<-formula(fit$mer,random=T)
y<-out[do.call(match,arg=list(x=x,table=c("1 | nest","1 | year")))]

but this doesn't work.

jpsmith
  • 11,023
  • 5
  • 15
  • 36
  • 1
    This is unlikely to go anywhere unliess respondents can get access to dat2. If it's too large or proprietary then just use an example from the package. – IRTFM Jul 25 '23 at 00:43
  • What does "this doesnt work" mean? Did it throw an error, give an unexpected result, etc? If you make this issue reproducible, you will likely get better, faster help (see [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for tips). Good luck! – jpsmith Jul 25 '23 at 00:46
  • After thrashing around determining the classes and structures of the output of `gamm4` I gave up and decided you might be satisfied with surgery on the formula object. I tested my answer on the example in the `?gamm4` page. – IRTFM Jul 25 '23 at 02:10
  • I'm puzzled by your statement that these are "ggm4 objects". When I use `class` on an object created by a call to `gamm4::gamm4`, I get "list". – IRTFM Jul 25 '23 at 14:14

1 Answers1

1

If you are willing to work with the formula object then you can do this:

  f.reduced <- formula(fit$mer, random=TRUE)[[3]][-(1:2)] 

Why? ... you ask. The innards of formula objects accept list accessor functions. Let's say the formula name is Fmla. Fmla[1] is just the tilde, i.e. the infix version of formula function. (I think you omitted the LHS and the tilde from your posting.) Fmla[2] will be the LHS and Fmla[3] is the RHS. The you can further use negative indexing into [ to remove the leading two terms.

(I get the idea that this might be generally true of the class of object because you got the same two leading terms of the RHS as I did using a random formula that only had one term. Alternatively you could iteratively remove terms containing "X" if the smoothing terms were more numerous.)

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks for the suggestion. It is true that I can do this on this specific formula object, however the goal is to be able to rremove all of the parts of the formula that refer to smoothing terms from within another function. This requires that I be able to identify all of these smoothing terms (i.e. the indices -(1:2) in your code. Because it is a formula object, functions like match() etc don't work. – Bill Shipley Jul 25 '23 at 12:04