[R] retrieve path analysis coefficients (package agricolae)

Peter Ehlers ehlers at ucalgary.ca
Mon May 24 13:10:09 CEST 2010


On 2010-05-23 18:16, Zack Holden wrote:
> Dear list,
> I'd like to use path.analysis in the package agricolae in batch format on
> many files, retrieving the path coefficients for each run and appending them
> to a table. I don't see any posts in the help files about this package or
> the path.analysis package. I've tried creating an object out of the call to
> path.analysis, but no matter what I try, the function automatically prints
> the result. I'll be grateful for any assistance.
>

The beauty of R is that it is open source software giving
you access to the code.

The function agricolae::path.analysis is very simple.
Unfortunately, it doesn't return anything and just prints
its results. But that's easy to fix. Here's a modified
version that just removes all the printing stuff and instead
returns the values of the path coefficients which you can
extract with '$Coeff' (note the capital 'A' in path.Analysis):

path.Analysis <-
function (corr.x, corr.y)
{
     if (ncol(corr.y) > 1)
         corr.y <- t(corr.y)
     Direct <- solve(corr.x, corr.y)
     n <- ncol(corr.x)
     Coeff <- corr.x
     for (i in 1:n) {
         for (j in 1:n) {
             Coeff[i, j] <- Direct[j] * corr.x[i, j]
         }
     }
     invisible(list('Coeff'=Coeff))
}

The loops aren't really necessary, so here's a simpler version:

path.Analysis <-
function (corr.x, corr.y)
{
     if (ncol(corr.y) > 1)
         corr.y <- t(corr.y)
     Direct <- solve(corr.x, corr.y)
     n <- ncol(corr.x)
     Coeff <- t(corr.x * rep(Direct, n))
     invisible(list("Coeff" = Coeff))
}

   -Peter Ehlers

> Thanks in advance,
> Zack
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________



More information about the R-help mailing list