简体   繁体   中英

obtain derivative by spline interpolation

there is a series of x and y values I have (but not the function itself). I would like to get derivative of the unknown function by spline interpolation of the x and y values (getting the derivat...). My example EDITED

x<-c(1,2,3,4,5,6,7,8,9,10)
y<-c(0.1,0.3,0.8,0.9,0.91,0.93,0.95,0.98,0.99,0.999)

is it possible in R to interpolate and to get the functional form of the derivative? My problem is that I have only x and y values of a cdf function but would need to obtain the probability denisty function..so I want to get the derivative by spline interpolation...

The reason for the question is that I would need to obtain the pdf of that cdf so I am trying to spline interpolate the xy values of the cdf - please note that this is a simple example here and not a real cdf

I haven't found the functional form of restricted cubic splines to be particularly difficult to grasp after reading the explanation by Frank Harrell in his book: "Regression Modeling Strategies".

require(rms)

 df <- data.frame( x = c(1,2,3,4,5,6,7,8,9,10),
                   y =c(12,2,-3,5,6,9,8,10,11,10.5))
 ols( y ~ rcs(x, 3), df)
#--------------
Linear Regression Model

ols(formula = y ~ rcs(x, 3), data = df)

                Model Likelihood     Discrimination    
                   Ratio Test           Indexes        
Obs       10    LR chi2      3.61    R2       0.303    
sigma 4.4318    d.f.            2    R2 adj   0.104    
d.f.       7    Pr(> chi2) 0.1646    g        2.811    

Residuals

    Min      1Q  Median      3Q     Max 
-8.1333 -1.1625  0.5333  0.9833  6.9000 

          Coef   S.E.   t    Pr(>|t|)
Intercept 5.0833 4.2431 1.20 0.2699  
x         0.0167 1.1046 0.02 0.9884  
x'        1.0000 1.3213 0.76 0.4738  
#----------

The rms package has an odd system for storing summary information that needs to be done for some of its special

 dd <- datadist(df)
 options(datadist="dd")
 mymod <- ols( y ~ rcs(x, 3), df)  
 # cannot imagine that more than 3 knots would make sense in such a small example
 Function(mymod)
# --- reformatted to allow inspection of separate terms
function(x = 5.5) {5.0833333+0.016666667* x +
                    1*pmax(x-5, 0)^3 - 
                    2*pmax(x-5.5, 0)^3 + 
                    1*pmax(x-6, 0)^3 }
<environment: 0x1304ad940>

The zeros in the pmax functions basically suppress any contribution to the total from the term when the x value is less than the knots ( 5, 5.5 and 6 in this case)

Compare three versus four knots (and if you wanted smooth curves then include a finer grained ...- data argument to Predict ):

 png()
 plot(df$x,df$y )
 mymod <- ols( y ~ rcs(x, 3), df)
 lines(df$x, predict(mymod) ,col="blue")
 mymod <- ols( y ~ rcs(x, 4), df)
 lines(df$x, predict(mymod) ,col="red")
dev.off()

在此处输入图片说明

Take a look at monotone cubic splines, which are nondecreasing by construction. A web search for "monotone cubic spline R" turns up some hits. I haven't used any of the packages mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM