Home > Blockchain >  How can I get this custom function to work with lapply?
How can I get this custom function to work with lapply?

Time:04-05

I'm trying to do a scale of effect analysis that uses the custom function found here: https://github.com/phuais/multifit/blob/master/multifit.R

It works fine for every list element of SAND_lc_list when I run each element separately, with no errors or warnings.

test <- multifit(mod = "glm", 
                    multief = buffer_names,     #column names
                    formula = presence ~ multief,
                    args = c("family = binomial"),
                    data = as.data.frame(SAND_lc_list[[9]]),      #which landcover variable
                    criterion = "AIC",
                    plot_est = T,
                    site_id = "Plot",
                    print_sum = TRUE)

The model including the effect 'r7500' was the best model according to the specified criterion (AIC, which.min)

Call:
glm(formula = presence ~ r7500, family = binomial, data = as.data.frame(SAND_lc_list[[9]]))

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-0.8244  -0.1537  -0.1160  -0.1051   3.1663  

Coefficients:
            Estimate Std. Error z value        Pr(>|z|)    
(Intercept)  -5.3623     0.7944  -6.750 0.0000000000148 ***
r7500         8.2955     2.5777   3.218         0.00129 ** 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 59.491  on 316  degrees of freedom
Residual deviance: 49.625  on 315  degrees of freedom
AIC: 53.625

Number of Fisher Scoring iterations: 7

However, when I try to use lapply to apply the function to each element of the list, I get the following error.

SAND_models <- lapply(SAND_lc_list, function(x) multifit(mod = "glm", 
                                                         multief = buffer_names,     #column names
                                                         formula = presence ~ multief,
                                                         args = c("family = binomial"),
                                                         data = as.data.frame(x),     
                                                         criterion = "AIC",
                                                         plot_est = T,
                                                         site_id = "Plot",
                                                         print_sum = TRUE))

Warning messages:
1: Fatal errors were found in all the models. Check them out by typing $Models to the generated object

> SAND_models$barren$models
$r200
<simpleError in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'as.data.frame': object 'x' not found>


I'm guessing I don't understand something about how lapply handles x - I never know if it is passing SAND_lc_list[i] or SAND_lc_list[[i]] to the function. I also wonder if it could have something to do with how the custom function is structured.

Any thoughts on how I could get this work?

CodePudding user response:

If the foreach is not working, then loop over the sequence and extract the object

lapply(seq_along(SAND_lc_list), function(i) {
      tmp <- as.data.frame(SAND_lc_list[[i]])
      multifit(mod = "glm", 
                    multief = buffer_names,     #column names
                    formula = presence ~ multief,
                    args = c("family = binomial"),
                    data = tmp,    
                    criterion = "AIC",
                    plot_est = TRUE,
                    site_id = "Plot",
                    print_sum = TRUE)})

If the object is using substitute, it can be an issue

out_lst <- vector('list', length(SAND_lc_list))
for(i in seq_along(SAND_lc_list)) {
    expr <- sprintf('multifit(mod = "glm", 
                    multief = buffer_names,     #column names
                    formula = presence ~ multief,
                    args = c("family = binomial"),
                    data = as.data.frame(SAND_lc_list[[%d]]),    
                    criterion = "AIC",
                    plot_est = TRUE,
                    site_id = "Plot",
                    print_sum = TRUE)', i)
    out_lst[[i]] <- eval(parse(text = expr))
}

  • Related