0

I wrote a few functions to fit some models and do plots on my old computer but they don't seem to work since I've started using the latest version of R (or of a package).

I'm having the same error message as this thread, but already point to the data I have loaded in the workspace, so the solution there doesn't work for me.

I'm working on a data from a survey in a data frame where the first row is column headers and the values are all integers. For the sake of the example we'll generate a 50x3 df - I've tried using data from different sources, I get the same problem, so this will work for the example.

I want to do a function which can take column names and a data frame name as input and output the summary of an lm() call and a plot. Using lm() or ggplot() on small_wb outside of a function works fine. But they won't work as part of a function. I can't see anything wrong with my syntax, is there something obvious I'm missing?

#########
## Load ggplot2
library(ggplot2)


#########
## Build a dataset 
set.seed(1)
small_df <- data.frame(Monnaie = sample(1:100, 50, replace = TRUE), Echange = sample(1:100, 50, replace = TRUE), Art = sample(1:100, 50, replace = TRUE))



###########
## Using summary() and lm() works
summary(lm(Art ~ Monnaie, small_df))

# Call:
#   lm(formula = Art ~ Monnaie, data = small_df)
# 
# Residuals:
#   Min      1Q  Median      3Q     Max 
# -36.256 -23.532  -8.721  20.025  61.364 
#
# etc etc etc


## Using ggplot() works too
ggplot(small_df, aes(Monnaie, Art)) +
  geom_point() +
  geom_smooth(method = "lm")

## ** awesome plot ** ##



###########
### Do some functions to test
## Plot lm
plot_lm <- function(i, j, wb){
  ggplot(wb, aes(i, j)) +
    geom_point() +
    geom_smooth(method = "lm")
}

## Summary lm
mod_lm <- function(i, j, wb) {
  summary(lm(j ~ i, data = wb))
}

## Plot + summary lm (this is what I want to achieve in the end, a function that does the plot + shows the data)
modplot_lm <- function(i, j, wb) {
  summary(lm(j ~ i, data = wb))
  plot_lm(i, j, wb)
}



##########
## Test the functions 
plot_lm(Monnaie, Art, small_df)
mod_lm(Monnaie, Art, small_df)
modplot_lm(Monnaie, Art, small_df)


#######
## With me, they all throw an error - here's what the terminal prints out (I included the traceback text where possible - though the problem varies between 'Art' not found and 'Monnaie' not found)

> plot_lm(Monnaie, Art, small_df)
Error in FUN(X[[i]], ...) : object 'Monnaie' not found

> mod_lm(Monnaie, Art, small_df)
 Hide Traceback

 Error in eval(predvars, data, env) : object 'Art' not found 
9.    eval(predvars, data, env) 
8.    eval(predvars, data, env) 
7.    model.frame.default(formula = j ~ i, data = wb, drop.unused.levels = TRUE) 
6.    stats::model.frame(formula = j ~ i, data = wb, drop.unused.levels = TRUE) 
5.    eval(mf, parent.frame()) 
4.    eval(mf, parent.frame()) 
3.    lm(j ~ i, data = wb) 
2.    summary(lm(j ~ i, data = wb)) 
1.    mod_lm(Monnaie, Art, small_df) 

> modplot_lm(Monnaie, Art, small_df)
 Hide Traceback

 Error in eval(predvars, data, env) : object 'Art' not found 
9.    eval(predvars, data, env) 
8.    eval(predvars, data, env) 
7.    model.frame.default(formula = j ~ i, data = wb, drop.unused.levels = TRUE) 
6.    stats::model.frame(formula = j ~ i, data = wb, drop.unused.levels = TRUE) 
5.    eval(mf, parent.frame()) 
4.    eval(mf, parent.frame()) 
3.    lm(j ~ i, data = wb) 
2.    summary(lm(j ~ i, data = wb)) 
1.    modplot_lm(Monnaie, Art, small_df) 

I tried the calls in functions separately to find the problem, but that they all don't work seems to me that the problem hasto do with the column names not being found in the function's environment.

Here's my session info

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_3.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.19     withr_2.1.2      crayon_1.3.4     dplyr_0.7.6      assertthat_0.2.0 grid_3.5.1       plyr_1.8.4      
 [8] R6_2.3.0         gtable_0.2.0     magrittr_1.5     scales_1.0.0     pillar_1.3.0     rlang_0.2.2      lazyeval_0.2.1  
[15] bindrcpp_0.2.2   labeling_0.3     tools_3.5.1      glue_1.3.0       purrr_0.2.5      munsell_0.5.0    compiler_3.5.1  
[22] pkgconfig_2.0.2  colorspace_1.3-2 tidyselect_0.2.5 bindr_0.1.1      tibble_1.4.2    
pandanotp
  • 41
  • 8
  • Forgot to add I got a new laptop so downloaded the latest versions of R studio and packages, this is when the functions stopped working, so the problem might also have happened during installation. I've already tried reinstalling a few packages (dplyr, ggplot2 etc.) but am still getting the errors – pandanotp Oct 14 '18 at 22:42
  • `?aes_string` || https://ggplot2.tidyverse.org/reference/aes_.html (note: there's no way "this just worked" on any of your systems). – hrbrmstr Oct 14 '18 at 22:44
  • Thanks that looks like it might help, I'll give it a try. The problem isn't that it "just worked", it's that i never updated anything on my old system and jumped to all the new versions with my new one, so I'm not sure if the problem is with that or something else – pandanotp Oct 14 '18 at 22:49
  • 1
    Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Oct 15 '18 at 00:00
  • Thanks Tung, those links really helped! I wasn't aware of these guidelines, it was really useful seeing them. I've edited my question, hope it's better now hrbmstr: I tried what you suggested but that didn't work. Maybe i'll find an answer now that my question is clearer! – pandanotp Oct 15 '18 at 17:11

0 Answers0