[R] using MAP and PURR to compute characteristic roots

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Thu Apr 1 02:42:11 CEST 2021


On 3/31/21 5:10 PM, Veerappa Chetty wrote:
> I have to compute characteristic roots for 100s of U.S.counties.  I got
> help using " lapply". I prefer using MAP and PURR if that is possible. I am
> using them to compute linear regressions.
> Here is my test problem and the output. I would appreciate any help.
> Thanks
> V.K.Chetty
> ____
> test.dat<- tibble(ID=c(1,2),a=c(1,1),b=c(1,1),c=c(2,2),d=c(4,3))
> test.out<-test.dat %>% nest(-ID) %>% mutate(fit = purrr::map(data,~
> function(x) eigen(data.matrix(x)), data=.))


I think you will find that there is a bias on R-help toward using "base" 
R functions where it seems "natural". The venue where "tidyverse" 
solutions are typically returned is StackOverflow.)

It wasn't stated  clearly what you wanted from this. The `eigen` 
function returns a list object so the apply function neatly handles row 
based operations:


 > apply( test.dat[-1], 1, function(x){eigen(matrix(x,2))})
[[1]]
eigen() decomposition
$values
[1] 4.5615528 0.4384472

$vectors
            [,1]       [,2]
[1,] -0.4896337 -0.9627697
[2,] -0.8719282  0.2703230


[[2]]
eigen() decomposition
$values
[1] 3.7320508 0.2679492

$vectors
            [,1]       [,2]
[1,] -0.5906905 -0.9390708
[2,] -0.8068982  0.3437238


If you instead wanted only the eigenvalues you might have chosen to 
extract them at the time of the `eigen` call:


  apply( test.dat[-1], 1, function(x){eigen(matrix(x,2))$values})
           [,1]      [,2]
[1,] 4.5615528 3.7320508
[2,] 0.4384472 0.2679492

Do note that the values are returned column-wise.

(I see that you noted in an earlier (duplicate) rhelp posting that you 
were expecting "younger" respondents. Since I'm in my eighth decade, I 
probably don't qualify.)


-- 

David Winsemius, MD


>
> I get this output:
>> test.out$fit
> [[1]]
> function(x) eigen(data.matrix(x))
> <environment: 0x0000017d35f00518>
>
> [[2]]
> function(x) eigen(data.matrix(x))
> <environment: 0x0000017d35ef73d0>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list