[R] AIC in MuMIn

Gavin Simpson gavin.simpson at ucl.ac.uk
Thu Aug 19 10:12:47 CEST 2010


On Thu, 2010-08-19 at 04:42 +0800, elaine kuo wrote:
> Yes, I tried the example in the ?dredge and agreed that something else
> caused the mistake.
> 
> 
> Aside from the cause which takes time to clarify (16 explanatory
> variables in the model), 
> I would like to ask another question.
> 
> 
> Please kindly advise if it is possible to show the singular model with
> only one certain variable using the command subset. (or maybe others)
> I tried the command "subset=X3" but it returned multiple models
> including X3.
> 
> 
> The above demand might look unnecessary when visual inspection offers
> the solution with few exp. variables. 
> However, it is of great importance for recognizing AIC for each
> variable in the model selection with more than 10 variables.

No, there isn't. The object returned by dredge is a data frame so you
can subset it to your hearts content, but you'll need to select rows
where you specify that all your variables (except the one you are
interested in) are missing (NA). Without writing this all out, I don't
know of a quick way of doing this sort of subsetting.

In effect, you want

data(Cement)
lm1 <- lm(y ~ ., data = Cement)
dd <- dredge(lm1, subset = X1)

want <- with(dd, is.na(X) & is.na(X2) & is.na(X3) & is.na(X4))
want
## how many models selected?
sum(want)
## OK selected just 1, show it
dd[want, , drop = FALSE]

Oh, actually, I suppose you could automate this, so it will return all
models with single variable:

dd <- dredge(lm1)
parms <- !is.na(dd[, -c(1, (ncol(dd) - c(0:7)))])
want <- which(rowSums(parms) == 1)
dd[want, ]

Having said all this, I don't think this is a good way to do model
selection.

G

> 
> 
> Please kindly help and thank you in advance.
> 
> 
> Elaine
> 
> 
> code
> library(MuMIn)
> 
> 
> data(Cement)
> lm1  <-  lm(y  ~  .,  data  =  Cement)
> dd  <-  dredge(lm1,  beta  = FALSE,  eval  =  TRUE,  rank  =  "AIC")
> X3<-dredge(lm1,  rank  =  "AIC", subset=X3)
> 
> On Wed, Aug 18, 2010 at 9:18 PM, Gavin Simpson
> <gavin.simpson at ucl.ac.uk> wrote:
>         On Wed, 2010-08-18 at 21:11 +0800, elaine kuo wrote:
>         > A cause other than data based on standardized regression
>         > was identified.
>         > It is that the manual command added with target <- at the
>         left hand
>         > side.
>         
>         > C1 did not work but C2 did.
>         > C1 target<-dredge(mig.stds,  subset  =  temp_max)
>         >
>         >
>         > C2 dredge(mig.stds,  subset  =  temp_max)
>         
>         
>         Glad you have this working, but that can't possibly be the
>         reason for
>         the error. There must be something else going on. The only
>         difference
>         between the two calls is that you are storing the result
>         somewhere, yet
>         the error was coming from within the running of the dredge
>         function.
>         
>         If everything works from within a new, clean R session then
>         great.
>         
>         
>         G
>         
>         >
>         >
>         > Elaine
>         >
>         > On Wed, Aug 18, 2010 at 5:37 PM, elaine kuo
>         <elaine.kuo.tw at gmail.com>
>         > wrote:
>         >
>         >
>         >                 >
>         >                 Please suggest how to define subset in my
>         case
>         >
>         >
>         >                 How would I know? I still haven't seen your
>         data. You
>         >                 seem to be
>         >                 mistaken on what is and is not included in
>         your model
>         >                 and you fitted it.
>         >                 What hope do we have...? However, given the
>         model
>         >                 'mig.stds' from above
>         >                 in this email:
>         >
>         >                 > mig.stds <-lm(SummerM_ratio ~ temp_max +
>         evi_mean +
>         >                 topo_var +
>         >                 >              topo_mean + coast +
>         Iso_index_0808,
>         >                 >              ## now tell R were to find
>         the
>         >                 variables in formula
>         >                 >              data = datum.std)
>         >                 > ## If you are fitting a Gaussian GLM it is
>         better
>         >                 fitted with lm()
>         >
>         >
>         >                 If you want to consider dredged models
>         containing
>         >                 temp_max, then you
>         >                 would do
>         >
>         >                 dredge(mig.stds, subset = temp_max)
>         >
>         >                 If you want models that contain temp_max and
>         coast,
>         >                 then you'd do
>         >
>         >                 dredge(mig.stds, subset = temp_max & coast)
>         >
>         >                 or
>         >
>         >                 dredge(mig.stds, fixed = ~ temp_max + coast)
>         >
>         >                 The bits you include in subset or fixed are
>         the names
>         >                 of your variables
>         >                 that you want in or out of the models. In
>         your case,
>         >                 the names of the
>         >                 variables as input into the model formula.
>         With
>         >                 'subset' you need to use
>         >                 logical operators (and [&], or [|]) whilst
>         with
>         >                 'fixed' you can specify
>         >                 a formula of variables that should be
>         included or
>         >                 excluded in the same
>         >                 way you'd write any R formula.
>         >
>         >                 But, now having been told this, please note
>         that this
>         >                 is *all* discussed
>         >                 on the ?dredge help page if you bother to
>         read it.
>         >                 I've never used this
>         >                 package, and, OK, I have used R for going on
>         for 11 or
>         >                 12 years now so
>         >                 am used to reading help pages and understand
>         the
>         >                 language a bit more you
>         >                 perhaps do, but you do seem to be asking
>         questions or
>         >                 running into
>         >                 problems that are all covered by the help
>         pages.
>         >
>         >
>         >                 => I posted it for help, after following the
>         manual
>         >                 with the command dredge but receiving an
>         error message
>         >                 two days ago.
>         >
>         >                  command   target<-dredge(mig.stds,  subset
>          =
>         >                  temp_max)
>         >            error  in eval(expr, envir, enclos) : 'temp_max'
>         not found
>         >
>         >
>         >          One possible cause could be data = datam.std.
>         >          datam.std was produced as the code below, which
>         seemed to
>         >         make it hard to find explanatory variables.
>         >          Please kindly share your experience in R, because I
>         am unsure
>         >         if my assumption is logical or not.
>         >
>         >
>         >         Also, please kindly advise how to modify the command
>         for
>         >         dredging subset if possible.
>         >         ( command   target<-dredge(mig.stds,  subset  =
>          temp_max))
>         >         Thank you in advance.
>         >
>         >
>         >         Elaine
>         >
>         >
>         >         code
>         >         library(MuMIn)
>         >         datam
>         >
>         <-read.csv("c:/migration/Mig_ratio_20100817.csv",header=T,
>         >         row.names=1)
>         >
>         >          # std regression model (16 indep. variables)
>         >           datam.sd<-scale(datam)
>         >           datam.std<-as.data.frame(datam.sd)
>         >           summary (datam.std)
>         >           mean(datam.std)
>         >
>         >
>         >         # obtain standard deviation
>         >             sd(datam.std)
>         >
>         >
>         >             mig.stds <-lm(SummerM_ratio~temp_ran+temp_mean
>         +temp_max
>         >         +temp_min+evi_ran+evi_mean+evi_max+evi_min+prec_ran
>         +prec_mean
>         >         +prec_max+prec_min+topo_var+topo_mean+coast
>         >         +Iso_index_0808,data=datam.std)
>         >
>         >             summary(mig.stds)
>         >
>         >
>         >
>         >
>         >
>         >
>         
>         
>         
>         --
>         %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~
>         %~%~%~%
>          Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
>          ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
>          Pearson Building,             [e]
>         gavin.simpsonATNOSPAMucl.ac.uk
>          Gower Street, London          [w]
>         http://www.ucl.ac.uk/~ucfagls/
>          UK. WC1E 6BT.                 [w]
>         http://www.freshwaters.org.uk
>         %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~
>         %~%~%~%
>         
>         
> 
> 

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list