[R-sig-Geo] Some comments on calculations with 'asc' (and 'kasc') objects with Adehabitat

Mathieu Basille basille at biomserv.univ-lyon1.fr
Sun Mar 27 15:17:00 CEST 2005

```Dear Sander,

Calculations with objects of class 'asc' and 'kasc' are quite simple, as
soon as you know that 'asc' ARE actually matrices and 'kasc' data
frames. The difference is just that they have a few additional
parameters ('xll', 'yll', 'cellsize' and 'type' - eventually 'levels' -
for both and 'nrow' and 'ncol' for kasc). Note that the first value of
the matrix (top-left corner) is actually the bottom-left corner of the
map (clockwise rotation of 90° between the map and the matrix). Note
also that each column of a 'kasc' represent one map (one variable). An
illustration :

data(puechabon)
kasc <- puechabon\$kasc
asc <- getkasc(kasc, "Elevation")
is.matrix(asc)
# [1] TRUE
is.data.frame(kasc)
# [1] TRUE

Then, the calculations are exactly the same as for matrices or data
frames. An exemple with a simple matrix :

m <- matrix(1:20, nr = 5)
mean(m)
# [1] 10.5
var(m)
#      [,1] [,2] [,3] [,4]
# [1,]  2.5  2.5  2.5  2.5
# [2,]  2.5  2.5  2.5  2.5
# [3,]  2.5  2.5  2.5  2.5
# [4,]  2.5  2.5  2.5  2.5

The 'var' function applied to a matrix gives the variance-covariance
matrix. To avoid that, just type :

var(as.vector(m))
# [1] 35

sd(m)
# [1] 1.581139 1.581139 1.581139 1.581139

The 'sd' function applied to a matrix returns a vector of the standard
deviation of the columns. To avoid that :

sd(as.vector(m))
# [1] 5.91608

The same with a simple data frame :

df <- cbind(a = 1:20, b = runif(20)*100, c = rnorm(20, 50, 10))
mean(df)
# [1] 38.64825

The 'mean' function gives the overall mean of the data frame. As for
kasc we obviously need means for the columns, we can try :

colMeans(df)
#        a        b        c
# 10.50000 56.74289 48.70187

colSums(df)
#        a         b         c
# 210.0000 1134.8577  974.0374

For the variance or standard deviation, use the 'apply' function :

apply(df, 2, var)
#        a         b         c
# 35.00000 748.78361  68.40536

apply(df, 2, sd)
#        a         b         c
# 5.916080 27.363911  8.270753

Now, the same, but applied to a 'real' asc object.

mean(asc)
# [1] NA
var(as.vector(asc))
# Error in var(as.vector(asc)) : missing observations in cov/cor
sd(as.vector(asc))
# Error in var(x, na.rm = na.rm) : missing observations in cov/cor

For all these functions, you get something wrong because of the NAs. Use
then the parameter na.rm = TRUE :

mean(asc, na.rm = TRUE)
# [1] 240.6568
var(as.vector(asc), na.rm = TRUE)
# [1] 7369.139
sd(as.vector(asc), na.rm = TRUE)
# [1] 85.84369

And with a kasc object :

colSums(kasc)
# Error in colSums(x, n, prod(dn), na.rm) : `x' must be numeric

You get an error because the variable 'Aspect' is a factor. We don't
need to compute means and others statistics to this variable.

colSums(kasc[-2])
#  Elevation      Slope Herbaceous
#         NA         NA         NA

Again, the same problem with the NAs, solved with the na.rm parameter :

colSums(kasc[-2], na.rm = TRUE)
#   Elevation       Slope  Herbaceous
# 1053836.000   40881.457    1905.650

colMeans(kasc[-2], na.rm = TRUE)
#   Elevation       Slope  Herbaceous
# 240.6567710   9.3357975   0.4351792

apply(kasc[-2], 2, var, na.rm = TRUE)
#    Elevation        Slope   Herbaceous
# 7.369139e+03 6.208515e+01 5.703959e-02

apply(kasc[-2], 2, sd, na.rm = TRUE)
#  Elevation      Slope Herbaceous
# 85.8436875  7.8794130  0.2388296

Finally, check the usefull functions 'summary' as applied to asc and kasc :

summary(as.vector(asc))
#    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's
#    65.0   176.0   256.0   240.7   290.0   479.0  9052.0

summary(kasc)
#    Elevation            Aspect         Slope
Herbaceous         #  Min.   :  65.0   NorthEast: 537   Min.   :
0.000   Min.   :   0.0000    #  1st Qu.: 176.0   SouthEast:1504   1st
Qu.:   3.124   1st Qu.:   0.2000    #  Median : 256.0   SouthWest:1262
Median :   6.658   Median :   0.4222    #  Mean   : 240.7
NorthWest:1076   Mean   :   9.336   Mean   :   0.4352    #  3rd Qu.:
290.0   NA's     :9052   3rd Qu.:  13.710   3rd Qu.:   0.7000    #
Max.   : 479.0                    Max.   :  40.631   Max.   :   0.7000
#  NA's   :9052.0                    NA's   :9052.000   NA's
:9052.0000