PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/RdotNET/bin/lib/R/library/mgcv/html/te.html

http://rdotnet.googlecode.com/
HTML | 254 lines | 221 code | 33 blank | 0 comment | 0 complexity | d324c6d0b3bc90d404081bad74989d48 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0, BSD-3-Clause, AGPL-3.0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html><head><title>R: Define tensor product smooths in GAM formulae</title>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. <link rel="stylesheet" type="text/css" href="../../R.css">
  5. </head><body>
  6. <table width="100%" summary="page for te {mgcv}"><tr><td>te {mgcv}</td><td align="right">R Documentation</td></tr></table>
  7. <h2>Define tensor product smooths in GAM formulae</h2>
  8. <h3>Description</h3>
  9. <p>
  10. Function used in definition of tensor product smooth terms within
  11. <code>gam</code> model formulae. The function does not evaluate a
  12. smooth - it exists purely to help set up a model using tensor product
  13. based smooths.
  14. </p>
  15. <h3>Usage</h3>
  16. <pre>te(..., k=NA,bs="cr",m=0,d=NA,by=NA,fx=FALSE,mp=TRUE,np=TRUE)
  17. </pre>
  18. <h3>Arguments</h3>
  19. <table summary="R argblock">
  20. <tr valign="top"><td><code>...</code></td>
  21. <td>
  22. a list of variables that are the covariates that this
  23. smooth is a function of.</td></tr>
  24. <tr valign="top"><td><code>k</code></td>
  25. <td>
  26. the dimension(s) of the bases used to represent the smooth term.
  27. If not supplied then set to <code>5^d</code>. If supplied as a single number then this
  28. basis dimension is used for each basis. If supplied as an array then the elements are
  29. the dimensions of the component (marginal) bases of the tensor
  30. product. See <code><a href="choose.k.html">choose.k</a></code> for further information.</td></tr>
  31. <tr valign="top"><td><code>bs</code></td>
  32. <td>
  33. array (or single character string) specifying the type for each
  34. marginal basis. <code>"cr"</code> for cubic regression spline; <code>"cs"</code> for cubic
  35. regression spline with shrinkage; <code>"cc"</code> for periodic/cyclic
  36. cubic regression spline; <code>"tp"</code> for thin plate regression spline;
  37. <code>"ts"</code> for t.p.r.s. with extra shrinkage. User defined bases can
  38. also be used here (see <code><a href="smooth.construct.html">smooth.construct</a></code> for an example). If only one
  39. identifier is given then this is used for all bases.</td></tr>
  40. <tr valign="top"><td><code>m</code></td>
  41. <td>
  42. The order of the penalty for each t.p.r.s. term (e.g. 2 for
  43. normal cubic spline penalty with 2nd derivatives). If a single number is given
  44. then it is used for all terms. <code>0</code> autoinitializes. <code>m</code> is ignored for the
  45. <code>"cr"</code> and <code>"cc"</code> bases.</td></tr>
  46. <tr valign="top"><td><code>d</code></td>
  47. <td>
  48. array of marginal basis dimensions. For example if you want a smooth for 3 covariates
  49. made up of a tensor product of a 2 dimensional t.p.r.s. basis and a 1-dimensional basis, then
  50. set <code>d=c(2,1)</code>.</td></tr>
  51. <tr valign="top"><td><code>by</code></td>
  52. <td>
  53. specifies a covariate by which the whole smooth term is to
  54. be multiplied. This is particularly useful for creating models in
  55. which a smooth interacts with a factor: in this case the <code>by</code>
  56. variable would usually be the dummy variable coding one level of the
  57. factor. See the examples below.</td></tr>
  58. <tr valign="top"><td><code>fx</code></td>
  59. <td>
  60. indicates whether the term is a fixed d.f. regression
  61. spline (<code>TRUE</code>) or a penalized regression spline (<code>FALSE</code>).</td></tr>
  62. <tr valign="top"><td><code>mp</code></td>
  63. <td>
  64. <code>TRUE</code> to use multiple penalties for the smooth. <code>FALSE</code> to use only
  65. a single penalty: single penalties are not recommended - they tend to allow only rather
  66. wiggly models.</td></tr>
  67. <tr valign="top"><td><code>np</code></td>
  68. <td>
  69. <code>TRUE</code> to use the `normal parameterization' for a tensor
  70. product smooth. This parameterization represents any 1-d marginal smooths
  71. using a parameterization where the parameters are function values at `knots'
  72. spread evenly through the data. The parameterization makes the penalties
  73. easily interpretable, however it can reduce numerical stability.</td></tr>
  74. </table>
  75. <h3>Details</h3>
  76. <p>
  77. Smooths of several covariates can be constructed from tensor products of the bases
  78. used to represent smooths of one (or sometimes more) of the covariates. To do this `marginal' bases
  79. are produced with associated model matrices and penalty matrices, and these are then combined in the
  80. manner described in <code><a href="tensor.prod.model.matrix.html">tensor.prod.model.matrix</a></code> and <code><a href="tensor.prod.model.matrix.html">tensor.prod.penalties</a></code>, to produce
  81. a single model matrix for the smooth, but multiple penalties (one for each marginal basis). The basis dimension
  82. of the whole smooth is the product of the basis dimensions of the marginal smooths.
  83. </p>
  84. <p>
  85. An option for operating with a single penalty (The Kronecker product of the marginal penalties) is provided, but
  86. it is rarely of practical use: the penalty is typically so rank deficient that even the smoothest resulting model
  87. will have rather high estimated degrees of freedom.
  88. </p>
  89. <p>
  90. Tensor product smooths are especially useful for representing functions of covariates measured in different units,
  91. although they are typically not quite as nicely behaved as t.p.r.s. smooths for well scaled covariates.
  92. </p>
  93. <p>
  94. Note also that GAMs constructed from lower rank tensor product smooths are
  95. nested within GAMs constructed from higher rank tensor product smooths if the
  96. same marginal bases are used in both cases (the marginal smooths themselves
  97. are just special cases of tensor product smooths.)
  98. </p>
  99. <p>
  100. The `normal parameterization' (<code>np=TRUE</code>) re-parameterizes the marginal
  101. smooths of a tensor product smooth so that the parameters are function values
  102. at a set of points spread evenly through the range of values of the covariate
  103. of the smooth. This means that the penalty of the tensor product associated
  104. with any particular covariate direction can be interpreted as the penalty of
  105. the appropriate marginal smooth applied in that direction and averaged over
  106. the smooth. Currently this is only done for marginals of a single
  107. variable. This parameterization can reduce numerical stability for when used
  108. with marginal smooths other than <code>"cc"</code>, <code>"cr"</code> and <code>"cs"</code>: if
  109. this causes problems, set <code>np=FALSE</code>.
  110. </p>
  111. <p>
  112. The function does not evaluate the variable arguments.
  113. </p>
  114. <h3>Value</h3>
  115. <p>
  116. A class <code>tensor.smooth.spec</code> object defining a tensor product smooth
  117. to be turned into a basis and penalties by the <code>smooth.construct.tensor.smooth.spec</code> function.
  118. <br>
  119. The returned object contains the following items:
  120. </p>
  121. <table summary="R argblock">
  122. <tr valign="top"><td><code>margin</code></td>
  123. <td>
  124. A list of <code>smooth.spec</code> objects of the type returned by <code><a href="s.html">s</a></code>,
  125. defining the basis from which the tensor product smooth is constructed.</td></tr>
  126. <tr valign="top"><td><code>term</code></td>
  127. <td>
  128. An array of text strings giving the names of the covariates that
  129. the term is a function of.</td></tr>
  130. <tr valign="top"><td><code>by</code></td>
  131. <td>
  132. is the name of any <code>by</code> variable as text (<code>"NA"</code> for none).</td></tr>
  133. <tr valign="top"><td><code>fx</code></td>
  134. <td>
  135. logical array with element for each penalty of the term
  136. (tensor product smooths have multiple penalties). <code>TRUE</code> if the penalty is to
  137. be ignored, <code>FALSE</code>, otherwise. </td></tr>
  138. <tr valign="top"><td><code>full.call</code></td>
  139. <td>
  140. Text for pasting into a string to be converted to a
  141. gam formula, which has the values of function options given explicitly -
  142. this is useful for constructing a fully expanded gam formula which can
  143. be used without needing access to any variables that may have been used
  144. to define k, fx, bs or m in the original call. i.e. this is text which
  145. when parsed and evaluated generates a call to <code>s()</code> with all the
  146. options spelled out explicitly.</td></tr>
  147. <tr valign="top"><td><code>label</code></td>
  148. <td>
  149. A suitable text label for this smooth term.</td></tr>
  150. <tr valign="top"><td><code>dim</code></td>
  151. <td>
  152. The dimension of the smoother - i.e. the number of
  153. covariates that it is a function of.</td></tr>
  154. <tr valign="top"><td><code>mp</code></td>
  155. <td>
  156. <code>TRUE</code> is multiple penalties are to be used (default).</td></tr>
  157. <tr valign="top"><td><code>np</code></td>
  158. <td>
  159. <code>TRUE</code> to re-parameterize 1-D marginal smooths in terms of function
  160. values (defualt).</td></tr>
  161. </table>
  162. <h3>Author(s)</h3>
  163. <p>
  164. Simon N. Wood <a href="mailto:simon.wood@r-project.org">simon.wood@r-project.org</a>
  165. </p>
  166. <h3>References</h3>
  167. <p>
  168. Wood, S.N. (2006) Low rank scale invariant tensor product smooths for
  169. Generalized Additive Mixed Models. Biometrics
  170. </p>
  171. <p>
  172. <a href="http://www.maths.bath.ac.uk/~sw283/">http://www.maths.bath.ac.uk/~sw283/</a>
  173. </p>
  174. <h3>See Also</h3>
  175. <p>
  176. <code><a href="s.html">s</a></code>,<code><a href="gam.html">gam</a></code>,<code><a href="gamm.html">gamm</a></code>
  177. </p>
  178. <h3>Examples</h3>
  179. <pre>
  180. # following shows how tensor pruduct deals nicely with
  181. # badly scaled covariates (range of x 5% of range of z )
  182. test1&lt;-function(x,z,sx=0.3,sz=0.4)
  183. { x&lt;-x*20
  184. (pi**sx*sz)*(1.2*exp(-(x-0.2)^2/sx^2-(z-0.3)^2/sz^2)+
  185. 0.8*exp(-(x-0.7)^2/sx^2-(z-0.8)^2/sz^2))
  186. }
  187. n&lt;-500
  188. old.par&lt;-par(mfrow=c(2,2))
  189. x&lt;-runif(n)/20;z&lt;-runif(n);
  190. xs&lt;-seq(0,1,length=30)/20;zs&lt;-seq(0,1,length=30)
  191. pr&lt;-data.frame(x=rep(xs,30),z=rep(zs,rep(30,30)))
  192. truth&lt;-matrix(test1(pr$x,pr$z),30,30)
  193. f &lt;- test1(x,z)
  194. y &lt;- f + rnorm(n)*0.2
  195. b1&lt;-gam(y~s(x,z))
  196. persp(xs,zs,truth);title("truth")
  197. vis.gam(b1);title("t.p.r.s")
  198. b2&lt;-gam(y~te(x,z))
  199. vis.gam(b2);title("tensor product")
  200. b3&lt;-gam(y~te(x,z,bs=c("tp","tp")))
  201. vis.gam(b3);title("tensor product")
  202. par(old.par)
  203. test2&lt;-function(u,v,w,sv=0.3,sw=0.4)
  204. { ((pi**sv*sw)*(1.2*exp(-(v-0.2)^2/sv^2-(w-0.3)^2/sw^2)+
  205. 0.8*exp(-(v-0.7)^2/sv^2-(w-0.8)^2/sw^2)))*(u-0.5)^2*20
  206. }
  207. n &lt;- 500
  208. v &lt;- runif(n);w&lt;-runif(n);u&lt;-runif(n)
  209. f &lt;- test2(u,v,w)
  210. y &lt;- f + rnorm(n)*0.2
  211. # tensor product of a 2-d thin plate regression spline and 1-d cr spline
  212. b &lt;- gam(y~te(v,w,u,k=c(30,5),d=c(2,1),bs=c("tp","cr")))
  213. op &lt;- par(mfrow=c(2,2))
  214. vis.gam(b,cond=list(u=0),color="heat",zlim=c(-0.2,3.5))
  215. vis.gam(b,cond=list(u=.33),color="heat",zlim=c(-0.2,3.5))
  216. vis.gam(b,cond=list(u=.67),color="heat",zlim=c(-0.2,3.5))
  217. vis.gam(b,cond=list(u=1),color="heat",zlim=c(-0.2,3.5))
  218. par(op)
  219. </pre>
  220. <hr><div align="center">[Package <em>mgcv</em> version 1.3-19 <a href="00Index.html">Index]</a></div>
  221. </body></html>