Re: [R] Matrix oriented computing

From: John Fox <jfox_at_mcmaster.ca>
Date: Sat 27 Aug 2005 - 02:01:28 EST


Dear Mark,

For that matter, the loop isn't a whole a slower (on my 3GHz Win XP system):

> x <- c(0.005, 0.010, 0.025, 0.05, 0.1, 0.5, 0.9, 0.95, 0.975, 0.99, 0.995)
> df <- 1:1000
> system.time(mat <- sapply(x, qchisq, df), gcFirst = TRUE)
[1] 0.08 0.00 0.08 NA NA
>
> mat <- matrix(0, 1000, 11)
> system.time(for (i in 1:length(df)) mat[i,] <- qchisq(x, df[i]))
[1] 0.09 0.00 0.10 NA NA
>

Regards,
 John



John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Marc
> Schwartz (via MN)
> Sent: Friday, August 26, 2005 10:26 AM
> To: Peter Dalgaard
> Cc: r-help@stat.math.ethz.ch
> Subject: Re: [R] Matrix oriented computing
>
> On Fri, 2005-08-26 at 15:25 +0200, Peter Dalgaard wrote:
> > Marc Schwartz <MSchwartz@mn.rr.com> writes:
> >
> > > x <- c(0.005, 0.010, 0.025, 0.05, 0.1, 0.5, 0.9,
> > > 0.95, 0.975, 0.99, 0.995)
> > >
> > > df <- c(1:100)
> > >
> > > mat <- sapply(x, qchisq, df)
> > >
> > > > dim(mat)
> > > [1] 100 11
> > >
> > > > str(mat)
> > > num [1:100, 1:11] 3.93e-05 1.00e-02 7.17e-02 2.07e-01
> 4.12e-01 ...
> >
> > outer() is perhaps a more natural first try... It does give the
> > transpose of the sapply approach, though.
> >
> > round(t(outer(x,df,qchisq)),2)
> >
> > should be close. You should likely add dimnames.
>
>
>
> What I find interesting, is that I would have intuitively expected
> outer() to be faster than sapply(). However:
>
>
> > system.time(mat <- sapply(x, qchisq, df), gcFirst = TRUE)
> [1] 0.01 0.00 0.01 0.00 0.00
>
> > system.time(mat1 <- round(t(outer(x, df, qchisq)), 2),
> gcFirst = TRUE)
> [1] 0.01 0.00 0.01 0.00 0.00
>
> # No round() or t() to test for overhead
> > system.time(mat2 <- outer(x, df, qchisq), gcFirst = TRUE)
> [1] 0.01 0.00 0.02 0.00 0.00
>
>
> # Bear in mind the round() on mat1 above
> > all.equal(mat, mat1)
> [1] "Mean relative difference: 4.905485e-05"
>
> > all.equal(mat, t(mat2))
> [1] TRUE
>
>
> Even when increasing the size of 'df' to 1:1000:
>
>
> > system.time(mat <- sapply(x, qchisq, df), gcFirst = TRUE)
> [1] 0.16 0.01 0.16 0.00 0.00
>
> > system.time(mat1 <- round(t(outer(x, df, qchisq)), 2), gcFirst =
> TRUE)
> [1] 0.16 0.00 0.18 0.00 0.00
>
> > # No round() or t() to test for overhead
> > system.time(mat2 <- outer(x, df, qchisq), gcFirst = TRUE)
> [1] 0.16 0.01 0.17 0.00 0.00
>
>
>
> It also seems that, at least in this case, t() and round() do
> not add much overhead.
>
> Best regards,
>
> Marc
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Aug 27 02:09:11 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:59:24 EST