为什么lm的固定截距不适用于poly(raw = FALSE)

时间:2019-02-05 12:32:05

标签: r lm predict

为什么固定截距会导致巨大的负偏移?见红线。

形成文档?poly

  

返回或评估1到0的度的正交多项式   指定的点集x:这些点都正交于   阶数为0的常数多项式。

因此,我希望次数为0的多项式为截距。我想念什么?

plot(df$t, df$y)

# this is working as expected
model1 <- lm(y ~ -1 + poly(t, 10, raw = TRUE), data = df)
model2 <- lm(y ~ -1 + poly(t, 10, raw = FALSE), data = df)
model3 <- lm(y ~ poly(t, 10, raw = TRUE), data = df) # raw = FALSE gives similar results

nsamples <- 1000
new_df <- data.frame(t = seq(0, 96, length.out = nsamples))
new_df$y1 <- predict(model1, newdata = new_df)
new_df$y2 <- predict(model2, newdata = new_df)
new_df$y3 <- predict(model3, newdata = new_df)

plot(new_df$t, new_df$y1, type = "l", ylim = c(-0.5, 1))
lines(new_df$t, new_df$y2, col = "red")
lines(new_df$t, new_df$y3 + 0.05, col = "blue") # offest for visibilty added!!
lines(c(0, 96), -c(mean(df$y), mean(df$y)), col = "red")

编辑:我认为问题等同于“使用了什么正交多项式(公式)?”。文档中的参考书是一本非常古老的书-我无法理解。并且有很多不同的正交多项式,例如维基百科。

数据:

df <- structure(list(t = c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 
                           8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 
                           19.5, 20.5, 21.5, 22.5, 23.5, 24.5, 25.5, 26.5, 27.5, 28.5, 29.5, 
                           30.5, 31.5, 32.5, 33.5, 34.5, 35.5, 36.5, 37.5, 38.5, 39.5, 40.5, 
                           41.5, 42.5, 43.5, 44.5, 45.5, 46.5, 47.5, 48.5, 49.5, 50.5, 51.5, 
                           52.5, 53.5, 54.5, 55.5, 56.5, 57.5, 58.5, 59.5, 60.5, 61.5, 62.5, 
                           63.5, 64.5, 65.5, 66.5, 67.5, 68.5, 69.5, 70.5, 71.5, 72.5, 73.5, 
                           74.5, 75.5, 76.5, 77.5, 78.5, 79.5, 80.5, 81.5, 82.5, 83.5, 84.5, 
                           85.5, 86.5, 87.5, 88.5, 89.5, 90.5, 91.5, 92.5, 93.5, 94.5, 95.5), 
                     y = c(0.00561299852289513, 0.0117183653372723, 0.0171836533727228, 
                           0.0234367306745446, 0.0280157557853274, 0.0331856228458887, 0.0391432791728213, 
                           0.0438700147710487, 0.048793697685869, 0.0539635647464303, 0.0586903003446578, 
                           0.0630723781388479, 0.0681437715411128, 0.0732151649433777, 0.0780403741999015, 
                           0.0813884785819793, 0.085425898572132, 0.0896110290497292, 0.0934022648941408, 
                           0.0968980797636632, 0.0996061053668144, 0.103495814869522, 0.107631708517971, 
                           0.111176760216642, 0.115017232890202, 0.119350073855244, 0.124766125061546, 
                           0.131216149679961, 0.139586410635155, 0.148153618906942, 0.156080748399803, 
                           0.166814377154111, 0.177006400787789, 0.189118660758247, 0.202412604628262, 
                           0.217577548005908, 0.234318069916297, 0.249089118660758, 0.267355982274741, 
                           0.284539635647464, 0.301477104874446, 0.316100443131462, 0.332151649433776, 
                           0.346873461349089, 0.361792220580995, 0.376366322008863, 0.392220580994584, 
                           0.408173313638602, 0.424224519940916, 0.439192516001969, 0.454849827671098, 
                           0.471196454948301, 0.485622845888725, 0.500443131462334, 0.514869522402757, 
                           0.529148202855736, 0.544559330379124, 0.559773510585918, 0.576218611521418, 
                           0.593303791235844, 0.609010339734121, 0.623929098966027, 0.6397341211226, 
                           0.655489906450025, 0.669768586903003, 0.68493353028065, 0.698867552929591, 
                           0.713244707040867, 0.726095519448548, 0.74027572624323, 0.752584933530281, 
                           0.76903003446578, 0.781486952240276, 0.794091580502216, 0.804726735598227, 
                           0.818217626784835, 0.832742491383555, 0.845691777449532, 0.856179222058099, 
                           0.866075824716888, 0.875923190546529, 0.886952240275726, 0.896898079763663, 
                           0.906203840472674, 0.915755785327425, 0.923879862136878, 0.932693254554407, 
                           0.940768094534712, 0.949187592319055, 0.956523879862137, 0.964204825209257, 
                           0.971344165435746, 0.978532742491384, 0.986558345642541, 0.993205317577548, 1)), 
                class = "data.frame", row.names = c(NA, -96L))

1 个答案:

答案 0 :(得分:1)

只需考虑一条回归线。对于(x, y)数据,让xx = mean(x)yy = mean(y)。合身

y = b * (x - xx)

不同于拟合

y = a + b * (x - xx)

,并且a(截距)测量垂直偏移。此外,可以证明a = yy