Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

r - 在R中,如何用最小二乘法估算样条曲线?(In R, how to estimate the spline curve by Least Square method?)

n = 50
set.seed(100)
x = matrix(runif(n, -2, 2), nrow=n)
y = 2 + 0.75*sin(x) - 0.75*cos(x) + rnorm(n, 0, 0.2)

样条曲线模拟

In R, I want to estimate above spline function by Least Square method.

(在R中,我想通过最小二乘法估算上述样条函数。)

The following hint wants me to generate design matrix:

(以下提示希望我生成设计矩阵:)

nknots = 12
mat.tpb = function(x, nknots){
    n = length(x)
    knots = seq(-2, 2, len=nknots)
    zb = cbind(1, x ,x^2, x^3)
    zt = matrix(NA, nrow=n, ncol=nknots)
    for(i in 1:nknots){zt[,i] = ifelse(x>knots[i], (x-knots[i]) ^3, 0)}
    cbind(zb, zt)}

z = mat.tpb(x, nknots)

I simplify the above for easier to read:

(我简化以上内容以便于阅读:)

mat.tpb = function(x, 12){
    zb = cbind(1, x ,x^2, x^3)
    zt = matrix(NA, nrow=length(x), ncol=12)
    for(i in 1:12){zt[,i] = ifelse(x>seq(-2, 2, len=12)[i], (x-seq(-2, 2, len=12)[i])^3, 0)}
    cbind(zb, zt)}

z = mat.tpb(x, 12)

I think the "t_j" in function "g_k+j+1(x)" means "seq(-2, 2, len=12)" in hint.

(我认为函数“ g_k + j + 1(x)”中的“ t_j”在提示中表示“ seq(-2,2,len = 12)”。)

Then since

(然后因为)

> seq(-2, 2, len=12)
 [1] -2.0000000 -1.6363636 -1.2727273 -0.9090909 -0.5454545 -0.1818182  0.1818182  0.5454545  0.9090909  1.2727273  1.6363636  2.0000000'

So that I can extend the estimator function as

(这样我就可以将estimator函数扩展为) 我扩展的估计函数

Which in word is

(换句话说)

E(y|x)=λ_1×1+λ_2×x+λ_3×x^2+λ_4×x^3+

(E(y | x)=λ_1×1 +λ_2×x +λ_3×x ^ 2 +λ_4×x ^ 3 +)

λ_5×max?{x-(-2+(4/11)×0),0}^3+λ_6×max?{(x-(-2+(4/11)×1),0}^3+λ_7×max?{(x-(-2+(4/11)×2),0}^3+λ_8×max?{(x-(-2+(4/11)×3),0}^3+λ_9×max?{(x-(-2+(4/11)×4),0}^3+λ_10×max?{(x-(-2+(4/11)×5),0}^3+λ_11×max?{(x-(-2+(4/11)×6),0}^3+λ_12×max?{(x-(-2+(4/11)×7),0}^3+λ_13×max?{(x-(-2+(4/11)×8),0}^3+λ_14×max?{(x-(-2+(4/11)×9),0}^3+λ_15×max?{(x-(-2+(4/11)×10),0}^3+λ_16×max?{(x-(-2(4/11)×11),0}^3

(λ_5×max?{x-(-2+(4/11)×0),0} ^ 3 +λ_6×max?{(x-(-2+(4/11)×1),0} ^ 3 +λ_7×max?{(x-(-2+(4/11)×2),0} ^ 3 +λ_8×max?{(x-(-2+(4/11)×3),0} ^ 3 +λ_9×max?{(x-(-2+(4/11)×4),0} ^ 3 +λ_10×max?{(x-(-2+(4/11)×5), 0} ^ 3 +λ_11×max?{(x-(-2+(4/11)×6),0} ^ 3 +λ_12×max?{(x-(-2+(4/11)×7 ),0} ^ 3 +λ_13×max?{(x-(-2+(4/11)×8),0} ^ 3 +λ_14×max?{(x-(-2+(4/11) ×9),0} ^ 3 +λ_15×max?{(x-(-2+(4/11)×10),0} ^ 3 +λ_16×max?{(x-(-2(4/11 )×11),0} ^ 3)

After simplification, what I want want to estimate is

(简化后,我想估计的是) 经过简化

Which in word is

(换句话说)

λ_1×1+λ_2×x+λ_3×x^2+λ_4×x^3+ λ_5×max{?(x+2),0}^3+λ_6×max?{(x+18/11),0}^3+λ_7×max?{(x+14/11),0}^3+λ_8×max?{(x+10/11),0}^3+λ_9×max?{(x+6/11),0}^3+λ_10×max?{(x+2/11),0}^3+λ_11×max?{(x-2/11),0}^3+λ_12×max?{(x-6/11),0}^3+λ_13×max?{(x-10/11),0}^3+λ_14×max?{(x-14/11),0}^3+λ_15×max?{(x-18/11),0}^3+λ_16×max?{(x-2),0}^3

(λ_1×1 +λ_2×x +λ_3×x ^ 2 +λ_4×x ^ 3 +λ_5×max {?(x + 2),0} ^ 3 +λ_6×max?{(x + 18/11),0 } ^ 3 +λ_7×max?{(x + 14/11),0} ^ 3 +λ_8×max?{(x + 10/11),0} ^ 3 +λ_9×max?{(x + 6 / 11),0} ^ 3 +λ_10×max?{(x + 2/11),0} ^ 3 +λ_11×max?{(x-2 / 11),0} ^ 3 +λ_12×max?{{ x-6 / 11),0} ^ 3 +λ_13×max?{(x-10 / 11),0} ^ 3 +λ_14×max?{(x-14 / 11),0} ^ 3 +λ_15× max?{(x-18 / 11),0} ^ 3 +λ_16×max?{(x-2),0} ^ 3)

But I do not know why the hint wants me to generate design matrix,

(但是我不知道为什么提示要我生成设计矩阵,)

so I am just going to estimate the function I simplified.

(所以我只是估计一下我简化的功能。)

But it will like this:

(但是它会像这样:)

> summary(lm(y ~ x+I(x^2)+I(x^3)+I(max(x+2, 0))^3+ I(max(x+(18/11), 0))^3+ I(max(x+(14/11), 0))^3+ I(max(x+(10/11), 0))^3+ I(max(x+(6/11), 0))^3+ I(max(x+(2/11), 0))^3+ I(max(x-(2/11), 0))^3+ I(max(x-(6/11), 0))^3+ I(max(x-(10/11), 0))^3+ I(max(x-(14/11), 0))^3+ I(max(x-(18/11), 0))^3+ I(max(x-2, 0))^3))
Error in model.frame.default(formula = y ~ x + I(x^2) + I(x^3) + I(max(x +  : 
  variable lengths differ (found for 'I(max(x + 2, 0))')

Please tell me in R, what function or package I can use to estimate the function I extend,

(请在R中告诉我,我可以使用哪些函数或包来估算我扩展的函数,)

or you know what the hint means and have other way to estimate please also tell me.

(或者您知道提示的含义,并有其他估算方法,请也告诉我。)

Thank you very much.

(非常感谢你。)

  ask by abba translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...