Dear all,
I have written a function for calculating the volume of a tree (=trad) or
snag (=h?gst).
The included volume regreesion model includes ten parameter values, which
are tree species specific.
bj?rk.formh?jd.pars is an object which includes the parameter values
(parameter set) for birch (=bj?rk).
There is one row per tree in the data object.
> relev.kols[1:5, c("ObjektID", "CPYTradID",
"tree.spec", "Diameter")]
ObjektID CPYTradID tree.spec Diameter
1 2101 2101.2.1 spruce 2
2 2101 2101.2.2 spruce 7
3 2101 2101.2.3 pine 29
4 2101 2101.2.4 pine 38
5 2101 2101.2.5 spruce 9
My problem is that the function (below) does not choose new parameter set
for each row. Instead it now chooses (and uses) the spruce parameter set
for the whole data object (because there is spruce on the first row?).
Could someone please give a hint on what's wrong.
Thanks!
Sincerely,
Tord
relev.kols$vol<- st?volfunk(relev.kols, tradhogst = "trad")
st?volfunk<- function(datamat, tradhogst=c("trad",
"h?gst"))
{
# first pick out correct parameter sets. They depend on tree species
# if tree speciec is bjork (birch) -> choose
bjork.formh?jd.pars
# and bjork.h?jd.pars as pars1 and pars2, respectively
#
if(datamat$tree.spec == "Bj?rk") {
pars1<- bj?rk.formh?jd.pars
pars2<- bj?rk.h?jd.pars
}
# if tree species is pine...
if( datamat$tree.spec == "pine" ) {
pars1<- pine.formh?jd.pars
pars2<- pine.h?jd.pars
}
# spruce, l?rk, en, obest?mt barr, obest?mt trad
if(datamat$tree.spec == "spruce" | datamat$tree.spec
="L?rk" | datamat$tree.spec == "En" |
datamat$tree.spec == "Obestamt.barr" |
datamat$tree.spec == "Obestamt.trad") {
pars1<- spruce.formh?jd.pars
pars2<- spruce.h?jd.pars
pars2<- spruce.h?jd.pars
}
# ?vrigt l?v
if(datamat$tree.spec != "Bj?rk" &
datamat$tree.spec !"pine" & datamat$tree.spec !=
"spruce" &
datamat$tree.spec != "L?rk" & datamat$tree.spec !=
"En"
& datamat$tree.spec != "Obestamt.barr" &
datamat$tree.spec
!= "Obestamt.trad") {
pars1<- ?vrl?v.formh?jd.pars
pars2<- ?vrl?v.h?jd.pars
}
# then calculate volume for the tree (=trad) or snag (=h?gst)
if(tradhogst == "trad"){
tradvol<- exp(S?derberg(datamat, pars1)) *
(datamat$Diameter)^2 * pi / 40000
return(tradvol)
}
if(tradhogst== "h?gst"){
# som cylinder om h?jden ?r <3 m
if(datamat$LangdHojd<30){
h?gstvol<- ( (datamat$Diameter/2) /100) ^2 * pi
* (datamatris$LangdHojd/10)
}
# och som kapat trad om h?jden ?r h?gre
else
# f?rst r?knas ursprunglig h?jd ut
urspr.tradh?jd<- exp(S?derberg(datamat,
pars2))/10 # omr?knat till m
#h?jdkvot
i<- datamat$LangdHojd/10 /urspr.tradh?jd
# sedan ursprunglig volym
urspr.tradvol<- exp(S?derberg(datamat, pars1)) *
(datamat$Diameter)^2 * pi / 40000
# och s? till sist h?gstubbens volym
h?gstvol<- urspr.tradvol* (2*i - i^2)
return(h?gstvol)
}
}
> relev.kols[1:5, c("ObjektID", "CPYTradID",
"tree.spec", "Diameter", "vol")]
ObjektID CPYTradID tree.spec Diameter vol
1 2101 2101.2.1 spruce 2 0.0005896749
2 2101 2101.2.2 spruce 7 0.0164895879
3 2101 2101.2.3 pine 29 0.6840856989
4 2101 2101.2.4 pine 38 1.2839852602
5 2101 2101.2.5 spruce 9 0.0329307304