Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • MkSpline - understanding the spline variables

    - this is a question about the logic of splines rather than code, I wonder if you can help me better understand splines and how splines are working.

    i have a curve that looks like this and I insert knots at 8 and 10

    when I use -mkspline-
    eg

    Code:
    local knots_list 8 10 
     mkspline spline= var1, knot (`knots_list') cubic
    This only generates 1 spline variable
    Spline 1 = which is identical to var 1
    Why does this happen ?

    second question:
    my understanding of a spline is that stata takes the sections of the curve i.e the points <8 and >10…

    what happens to the section between 8 and 10 ?

    or perhaps I haven’t understood splines corectly

    Attached Files

  • #2
    I've looked once again at the stata help = I thinK i'VE FOund my mistake

    The file tates:

    nknots(#) is allowed only with the third syntax. It specifies the number of knots that are to be used
    for a RESTRICTED cubic spline. This number must be between 3 and 7 UNLESS the knot locations are
    specified using knots().

    Does this mean that I can still use knot values of 8 and 10 If i specify my knots using knots()

    i.e I should be using:

    Code:
    mkspline spline1 = var1, cubic [nknots(3) knots(8 10 16)]
    The mistake I was making in this code below that my knot value were being given 8 and 10 - which are 2 knots which is not allowed for restricted cubic splines. Is this correct?

    Code:
     
     local knots_list 8 10  mkspline spline= var1, knot (`knots_list') cubic

    Comment


    • #3

      I think this is what you want: mkspline spline1 = var1, cubic nknots(2) knots(8 10)

      Comment


      • #4
        In Stata 18, the command is makespline which has a different format. Not sure you want a cubic spline, but a linear spline.

        Code:
        capture program drop myreg
        program myreg, rclass
        tempvar x d0 d1 d2 y
        g `x' = rnormal()
        g `d0' = `x'<=0
        g `d1' = `x'>0 & `x'<=1
        g `d2' = `x'>1
        g `y' = 1 + 0.5*`x'*`d0' + 2*`x'*`d1' + 4*`x'*`d2' + rnormal(0,.1)
        makespline linear `x', knotslist(0 1)
        reg `y' `x' _sp_1_1 _sp_1_2 `d1' `d2'
        end
        clear
        set obs 1000
        simulate _b, reps(500) saving(results, replace): myreg
        summ *

        Comment


        • #5
          George Ford Thanks - isn’t post3 the same as my forst line of code in post2 ? Am I missing something?

          your post 2 code goes against the statement in the help file which states that to make a restricted cubic spline one needs to have a nknot(#) where # is a value between 3-7 .

          that’s why I added another value of ‘16’ as another knot point .
          Last edited by Denise Vella; 14 Sep 2023, 16:53.

          Comment


          • #6
            Yeah, I see that. Your figure is linear and fairly well defined, but not sure that's what you're really looking at.

            Comment

            Working...
            X