Hi, I want to compute the quantiles of Chi^2 distributions with different degrees of freedom like x<-cbind(0.005, 0.010, 0.025, 0.05, 0.1, 0.5, 0.9, 0.95, 0.975, 0.99, 0.995) df<-rbind(1:100) m<-qchisq(x,df) and hoped to get back a length(df) times length(x) matrix with the quantiles. Since this does not work, I use 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) m<-qchisq(x,df[1]) for (i in 2:length(df)) { m<-rbind(m,qchisq(x,df[i])) } dim(m)<-c(length(df),length(x)) Is there a way to avoid the for loop ? Thanks Sigbert
On Fri, 26 Aug 2005 14:44:10 +0200 Sigbert Klinke wrote:> Hi, > > I want to compute the quantiles of Chi^2 distributions with different > degrees of freedom like > > x<-cbind(0.005, 0.010, 0.025, 0.05, 0.1, 0.5, 0.9, 0.95, 0.975, 0.99, > 0.995) df<-rbind(1:100) > m<-qchisq(x,df) > > and hoped to get back a length(df) times length(x) matrix with the > quantiles. Since this does not work, I use > > 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) > m<-qchisq(x,df[1]) > for (i in 2:length(df)) { > m<-rbind(m,qchisq(x,df[i])) > } > dim(m)<-c(length(df),length(x)) > > Is there a way to avoid the for loop ?You could use sapply(): m <- sapply(x, function(x) qchisq(x, df)) hth, Z> Thanks Sigbert > > ______________________________________________ > R-help at 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 >
On Fri, 2005-08-26 at 14:44 +0200, Sigbert Klinke wrote:> Hi, > > I want to compute the quantiles of Chi^2 distributions with different > degrees of freedom like > > x<-cbind(0.005, 0.010, 0.025, 0.05, 0.1, 0.5, 0.9, 0.95, 0.975, 0.99, 0.995) > df<-rbind(1:100) > m<-qchisq(x,df) > > and hoped to get back a length(df) times length(x) matrix with the > quantiles. Since this does not work, I use > > 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) > m<-qchisq(x,df[1]) > for (i in 2:length(df)) { > m<-rbind(m,qchisq(x,df[i])) > } > dim(m)<-c(length(df),length(x)) > > Is there a way to avoid the for loop ? > > Thanks SigbertSee ?sapply 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 ... HTH, Marc Schwartz
I believe that the following is what you want: x <- c(0.005, 0.010, 0.025, 0.05, 0.1, 0.5, 0.9, 0.95, 0.975, 0.99, 0.995) dof <- 1:100 ans <- outer(x, dof, qchisq) dimnames(ans) <- list(x, dof) Note that 'df' is not a very auspicious name for an object since it is the name of a function. Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Sigbert Klinke wrote:>Hi, > >I want to compute the quantiles of Chi^2 distributions with different >degrees of freedom like > >x<-cbind(0.005, 0.010, 0.025, 0.05, 0.1, 0.5, 0.9, 0.95, 0.975, 0.99, 0.995) >df<-rbind(1:100) >m<-qchisq(x,df) > >and hoped to get back a length(df) times length(x) matrix with the >quantiles. Since this does not work, I use > >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) >m<-qchisq(x,df[1]) >for (i in 2:length(df)) { > m<-rbind(m,qchisq(x,df[i])) >} >dim(m)<-c(length(df),length(x)) > >Is there a way to avoid the for loop ? > >Thanks Sigbert > >______________________________________________ >R-help at 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 > > > > >