greta-dev / greta

simple and scalable statistical modelling in R
https://greta-stats.org
Other
531 stars 63 forks source link

apply not working on greta_array #514

Open hrlai opened 2 years ago

hrlai commented 2 years ago

Hi, I bumped into this weird problem today... hopefully there is a quick fix or I was just doing something wrong.

library(greta)
z <- normal(0, 1, dim = c(2, 3))
apply(z, 1, mean)    # doesn't work
rowMeans(z)          # works

Working on Ubuntu and greta_0.4.2.

hrlai commented 2 years ago

Okay it just got weirder... because apply(z, 1, "mean") works...

njtierney commented 2 years ago

That sounds spooky! Let me check this out.

hrlai commented 1 year ago

Hi there, I'm looking into cumsum and encountered a similar issue to the earlier one, potentially also related to #504

Wanted to apply the cumsum function across rows of a greta_array, but got NAs instead... do you think these fall into the same category of things to improve?

library(greta)
#> 
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#> 
#>     binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#> 
#>     %*%, apply, backsolve, beta, chol2inv, colMeans, colSums, diag,
#>     eigen, forwardsolve, gamma, identity, rowMeans, rowSums, sweep,
#>     tapply

x <- normal(0, 10, dim = c(4, 5))
#> ℹ Initialising python and checking dependencies, this may take a moment.
#> ✔ Initialising python and checking dependencies ... done!
#> 

apply(x, 1, mean)   # doesn't work
#> Error in match.arg(FUN): 'arg' must be NULL or a character vector
apply(x, 1, "mean") # works
#> greta array (operation)
#> 
#>      [,1]
#> [1,]  ?  
#> [2,]  ?  
#> [3,]  ?  
#> [4,]  ?

apply(x, 2, sd)     # doesn't work
#> Error in match.arg(FUN): 'arg' must be NULL or a character vector
apply(x, 2, "sd")   # doesn't work too, but gives more informative error
#> Error in match.arg(FUN): 'arg' should be one of "sum", "max", "mean", "min", "prod", "cumsum", "cumprod"

apply(x, 2, cumsum)   # doesn't work
#> Error in match.arg(FUN): 'arg' must be NULL or a character vector
apply(x, 2, "cumsum") # supposed to work according to error message above, but doesn't
#> greta array (operation)
#> 
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]   NA   NA   NA   NA   NA
#> [2,]   NA   NA   NA   NA   NA
#> [3,]   NA   NA   NA   NA   NA
#> [4,]   NA   NA   NA   NA   NA

Created on 2022-12-19 with reprex v2.0.2