Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Display spline knots with xline?


From   Ben Jann <[email protected]>
To   [email protected]
Subject   Re: st: Display spline knots with xline?
Date   Wed, 28 Jan 2009 16:34:02 +0100

> Official Mata has its own -invtokens()- function so that you can use
> that instead without installing -moremata-.

Right, I forgot about that. -invtokens()- was introduced with Stata 10.

Although it does not matter in the present case, note that there is a
subtle difference between -invtokens()- and -mm_invtokens()- in that
the latter preserves tokens that contain blanks. Example:

. mata
------------------------------------------------- mata (type end to
exit) -------
: invtokens(("a a", "b", "c"))
  a a b c

: mm_invtokens(("a a", "b", "c"))
  "a a" b c

: end
---------------------------------------------------------------------------------

On the other hand, -invtokens()- lets you set the separator, which is
not possible with -mm_invtokens()-:
. mata:
------------------------------------------------- mata (type end to
exit) -------
: invtokens(("a a", "b", "c"), ";")
  a a;b;c

: end
---------------------------------------------------------------------------------
ben

On Wed, Jan 28, 2009 at 3:25 PM, Nick Cox <[email protected]> wrote:
> Two points:
>
> Official Mata has its own -invtokens()- function so that you can use
> that instead without installing -moremata-. But note that Ben's function
> does pre-date the official one, or so I recall.
>
> Also, -rcspline- on SSC has an option -showknots- which does this. It
> may be that
>
> . rcspline price mpg, nknots(7) showknots
>
> is what you want. Alternatively, install -rcspline- anyway using -ssc-
> and look at the code for showing the knots.
>
>                if "`showknots'" != "" {
>                        tempname xk
>                        matrix `xk' = r(knots)
>                        forval i = 1/`= r(N_knots)' {
>                                local k `k' `=`xk'[1, `i']'
>                        }
>                        local showk xline(`k', lw(vvthin))
>                }
>
> Nick
> [email protected]
>
> Ben Jann
>
> How about:
>
> sysuse auto
> mkspline smpg=mpg, nknots(7) displayknots cubic
> matrix list r(knots)
> mata: st_local("knots", mm_invtokens(strofreal(st_matrix("r(knots)"))))
> scatter price mpg, xline(`knots')
>
> -moremata- is required. Type -ssc install moremata-.
> ben
>
> On Wed, Jan 28, 2009 at 12:13 PM, Claus Dethlefsen
> <[email protected]> wrote:
>
>> Using cubic splines, I would like to display the knots used. In Stata
>> 10, I can use mkspline to create the spline variables and it returns
>> the values of the knots in a matrix. I would like to transfer this
>> matrix in a nice way to my scatter plot command as the xline argument.
>> Example:
>>
>>
>> sysuse auto
>> mkspline smpg=mpg, nknots(7) displayknots cubic
>> matrix list r(knots)
>>
>>
>> scatter price mpg, xline(14         16         18         20
>> 23         26         31)
>>                                   ^ here I would like to write
>> r(knots) instead of copy-pasting the values
>>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index