using eigen function in MAP and purr

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

using eigen function in MAP and purr

Veerappa Chetty
I want to use map and purr functions to compute eigen values for 3000
matrices. Each matrix has 2 rows and 2 columns. The following code does not
work.

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(matrix(x,2,2)), data=.))

This must be a trivial question for current young practitioners ( In my 9
th decade, I am having fun using R markdown and I am trying to continue my
research!) I would greatly appreciate any help.
Thanks.
V.K.Chetty
--
Professor of Family Medicine
Boston University

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: using eigen function in MAP and purr

John Fox
Dear V. K. Chetty,

Perhaps I'm missing something but why wouldn't you just use a list of
matrices, as in the following?

---------- snip ---------

 > set.seed(123) # for reproducibility

 > (Matrices <- lapply(1:3, function(i) matrix(sample(1:50, 4), 2, 2)))

[[1]]
      [,1] [,2]
[1,]   31   14
[2,]   15    3

[[2]]
      [,1] [,2]
[1,]   42   37
[2,]   43   14

[[3]]
      [,1] [,2]
[1,]   25   27
[2,]   26    5

 > (Eigenvalues <- lapply(Matrices, function(x) eigen(x,
only.values=TRUE)$values))

[[1]]
[1] 37.149442 -3.149442

[[2]]
[1]  70.27292 -14.27292

[[3]]
[1]  43.3196 -13.3196

---------- snip ---------

I hope this helps,
  John

John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://socialsciences.mcmaster.ca/jfox/

On 2021-03-29 5:28 p.m., Veerappa Chetty wrote:

> I want to use map and purr functions to compute eigen values for 3000
> matrices. Each matrix has 2 rows and 2 columns. The following code does not
> work.
>
> 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(matrix(x,2,2)), data=.))
>
> This must be a trivial question for current young practitioners ( In my 9
> th decade, I am having fun using R markdown and I am trying to continue my
> research!) I would greatly appreciate any help.
> Thanks.
> V.K.Chetty
>

______________________________________________
[hidden email] 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.