我试图在R中使用b样条来找到函数的系数。然而,结果有些奇怪。我用R输出系数来找到y-hat,然后是平方误差,这太离谱了。我不知道我的计算出了什么问题,也不知道我在代码中遗漏了什么。
这是我的数据(有1000个观测结果(:
structure(list(age = c(18, 24, 45, 43, 50, 54, 44, 30, 41, 52,
45, 34, 35, 39, 54, 51, 37, 50, 56, 37, 38, 40, 75, 40, 38, 49,
43, 34, 57, 18, 55, 51, 33, 34, 36, 56, 70, 25, 32, 27, 28, 27,
43, 50, 39, 52, 35, 57, 25, 33, 57, 71, 43, 23, 30, 22, 59, 28,
61, 34, 43, 54, 69, 41, 48, 49, 42, 37, 55, 21, 58, 31, 25, 32,
40, 44, 60, 23, 63, 44, 47, 61, 55, 24, 42, 25, 34, 53, 53, 70,
47, 46, 33, 34, 22, 74, 40, 45, 43, 33, 62, 37, 54, 34, 50, 46,
41, 63, 38, 35, 29, 66, 37, 39, 42, 51, 55, 51, 38, 49, 42, 43,
38, 59, 57, 25, 49, 41, 38, 61, 49, 52, 43, 60, 46, 21, 61, 32,
58, 35, 26, 32, 37, 22, 51, 44, 35, 60, 40, 35, 35, 47, 43, 33,
60, 38, 53, 55, 57, 64, 43, 35, 54, 45, 58, 48, 46, 46, 55, 51,
49, 34, 53, 40, 50, 37, 39, 52, 50, 48, 47, 27, 39, 44, 37, 52,
26, 39, 25, 31, 58, 30, 27, 40, 55, 35, 48, 29, 25, 40, 27, 44,
49, 22, 45, 33, 63, 49, 39, 25, 29, 37, 35, 46, 58, 39, 41, 29,
37, 62, 27, 37, 56, 36, 23, 46, 26, 43, 36, 54, 62, 40, 53, 48,
23, 34, 43, 55, 59, 60, 49, 45, 31, 34, 26, 22, 40, 41, 36, 38,
48, 47, 47, 35, 49, 40, 32, 57, 56, 73, 29, 49, 30, 29, 36, 39,
48, 45, 50, 42, 58, 48, 48, 45, 41, 27, 44, 32, 46, 52, 22, 53,
34, 38, 42, 25, 29, 28, 46, 55, 56, 41, 61, 47, 52, 27, 33, 58,
27, 52, 45, 22, 25, 25, 41, 54, 23, 36, 31, 52, 46, 45, 40, 33,
42, 41, 25, 24, 24, 51, 41, 42, 30, 55, 23, 60, 40, 41, 80, 53,
40, 35, 55, 24, 39, 30, 68, 54, 27, 34, 53, 48, 59, 30, 45, 39,
45, 43, 30, 57, 53, 42, 23, 58, 43, 33, 56, 37, 41, 33, 50, 43,
53, 37, 20, 26, 29, 57, 47, 54, 38, 23, 31, 43, 33, 48, 33, 52,
30, 44, 38, 51, 47, 28, 52, 23, 65, 27, 33, 49, 27, 48, 38, 58,
40, 46, 53, 25, 42, 40, 35, 50, 34, 48, 49, 41, 37, 58, 28, 33,
50, 38, 48, 30, 49, 44, 50, 58, 45, 44, 35, 59, 45, 51, 61, 45,
34, 33, 45, 50, 50, 39, 26, 49, 20, 31, 49, 56, 61, 56, 51, 42,
45, 30, 48, 33, 50, 31, 48, 35, 47, 45, 52, 46, 47, 49, 36, 51,
46, 37, 23, 43, 34, 58, 44, 61, 40, 37, 33, 38, 41, 30, 51, 39,
56, 44, 51, 35, 27, 47, 51, 41, 63, 61, 46, 33, 35, 28, 50, 41,
23, 45, 63, 59, 30, 30, 45, 42, 57, 52, 54, 30, 47, 38, 47, 39,
50, 47, 45, 44, 52, 42, 18, 56, 50, 50, 53, 38, 27, 56, 50, 56,
50, 49, 42, 50, 53, 28, 45, 24, 25, 38, 47, 38, 33, 39, 52, 46,
42, 61, 32, 42, 61, 27, 45, 58, 25, 34, 44, 40, 51, 50, 64, 54,
42, 31, 25, 32, 51, 75, 28, 28, 32, 53, 36, 43, 51, 35, 46, 38,
43, 39, 37, 59, 33, 23, 37, 40, 25, 27, 47, 52, 23, 33, 37, 34,
51, 40, 50, 64, 54, 50, 32, 37, 19, 32, 42, 26, 46, 18, 43, 62,
44, 40, 43, 32, 48, 35, 61, 47, 55, 28, 52, 27, 22, 20, 42, 29,
31, 59, 36, 32, 32, 25, 43, 44, 57, 49, 41, 38, 58, 59, 39, 60,
56, 44, 70, 27, 47, 41, 29, 42, 51, 47, 48, 39, 23, 31, 59, 19,
42, 35, 38, 45, 58, 26, 30, 54, 38, 44, 52, 73, 65, 28, 53, 53,
52, 27, 57, 32, 51, 43, 50, 53, 54, 24, 40, 30, 45, 55, 45, 44,
37, 41, 44, 52, 55, 43, 27, 43, 46, 30, 43, 48, 33, 29, 24, 52,
50, 24, 42, 53, 49, 46, 40, 41, 41, 36, 34, 24, 44, 33, 25, 40,
48, 56, 25, 44, 27, 58, 52, 60, 39, 51, 31, 45, 43, 45, 40, 46,
51, 47, 52, 31, 40, 26, 49, 38, 50, 63, 35, 29, 33, 29, 62, 45,
56, 58, 39, 49, 24, 61, 58, 47, 41, 71, 47, 38, 31, 56, 44, 33,
36, 31, 55, 48, 50, 42, 25, 55, 28, 30, 46, 48, 42, 23, 60, 34,
45, 28, 33, 58, 47, 24, 56, 56, 23, 56, 45, 58, 30, 46, 48, 50,
43, 37, 49, 31, 54, 52, 70, 30, 44, 34, 38, 28, 38, 39, 52, 34,
33, 45, 58, 38, 63, 38, 31, 53, 21, 38, 18, 28, 33, 32, 29, 28,
42, 28, 36, 47, 53, 48, 36, 55, 37, 68, 65, 37, 33, 19, 50, 22,
40, 45, 36, 37, 24, 66, 35, 33, 44, 48, 47, 41, 55, 74, 33, 62,
56, 29, 56, 47, 45, 44, 25, 45, 40, 56, 56, 47, 39, 38, 48, 44,
42, 55, 61, 39, 30, 49, 33, 31, 35, 55, 36, 37, 33, 48, 47, 60,
28, 28, 48, 29, 40, 32, 33, 40, 42, 36, 39, 44, 43, 44, 29, 49,
47, 46, 41, 52, 43, 38, 42, 56, 39, 29, 33, 33, 41, 44, 45, 58,
29, 42, 68, 47, 30, 39, 42, 49, 36, 44, 54, 42, 39, 30, 33, 28,
38, 30, 25, 52, 26, 54, 62, 57, 32, 60, 40, 49, 54, 48, 35, 46,
42, 58, 30, 27, 55, 50, 55, 40, 32, 27, 49, 50, 50, 44, 57, 72,
23, 31, 36, 63, 48, 46, 55, 44, 44, 32, 60, 42, 48, 36, 31, 54,
29, 46, 35, 50, 25, 33, 34, 51, 33, 59, 62, 28, 40, 52), wage = c(75.0431540173515,
70.4760196469445, 130.982177377461, 154.68529299563, 75.0431540173515,
127.115743812184, 169.528538036679, 111.720849360989, 118.884359339886,
128.680488220624, 117.146816914805, 81.2832532842527, 89.4924795180001,
134.705375118879, 134.705375118879, 90.4819133566401, 82.6796372966372,
212.842352315711, 129.156693004704, 98.5993438603892, 200.543262324662,
50.406660861266, 85.3839403789827, 160.642475408263, 94.0727147457005,
277.601417511009, 84.0459576539924, 65.1108537153447, 116.265324062503,
70.4760196469445, 195.676307990453, 101.402052302992, 81.2832532842527,
111.720849360989, 89.8800467811106, 87.9810327856054, 87.9810327856054,
103.90247060316, 105.927810775063, 66.2294082943902, 34.6074177301288,
109.833985642199, 45.282169994022, 73.7757432746946, 81.2832532842527,
154.68529299563, 267.901086855275, 176.989650489877, 66.2294082943902,
116.265324062503, 200.543262324662, 101.82435207568, 272.294783220064,
35.0334213214995, 114.475713290347, 81.2832532842527, 200.543262324662,
148.413159102577, 99.6894636984864, 111.720849360989, 152.838800991182,
117.146816914805, 77.7376033039781, 87.9810327856054, 125.525498433836,
123.336103962059, 158.294621427791, 200.543262324662, 99.6894636984864,
70.4760196469445, 94.0727147457005, 81.2832532842527, 81.2832532842527,
100.764272842107, 94.0727147457005, 99.6894636984864, 268.266292012557,
81.2832532842527, 113.567089569751, 87.9810327856054, 128.680488220624,
106.723977625299, 66.0446942318056, 51.521956314716, 85.3839403789827,
76.8936025176112, 90.4819133566401, 68.0764282889521, 130.982177377461,
127.901232979118, 131.224593755096, 99.6894636984864, 76.8936025176112,
107.903923866876, 81.2832532842527, 99.6894636984864, 98.5993438603892,
200.543262324662, 156.503510882633, 144.944364980516, 118.884359339886,
121.430313822622, 111.720849360989, 68.7480877337669, 114.475713290347,
95.2307125669819, 87.9810327856054, 64.2743972206327, 128.680488220624,
114.475713290347, 112.6488963421, 81.2832532842527, 75.8954848228103,
112.136922992214, 85.3839403789827, 94.0727147457005, 114.475713290347,
176.989650489877, 276.778413321176, 101.82435207568, 91.6992261117042,
86.6951548289402, 162.946460744664, 139.213788138664, 95.2307125669819,
66.9616443217359, 131.194323611687, 90.4819133566401, 114.475713290347,
79.1280844956297, 104.591737924918, 132.487821126123, 141.77517233318,
123.089699847943, 134.705375118879, 55.7673905084923, 106.921814091211,
118.884359339886, 87.9810327856054, 88.9296066367913, 81.2832532842527,
94.0727147457005, 106.921814091211, 59.0970640815891, 65.1108537153447,
120.589436530402, 157.700527725737, 129.453644576625, 176.989650489877,
59.0970640815891, 94.0727147457005, 134.705375118879, 81.2832532842527,
81.2832532842527, 153.457515308961, 136.876367635184, 75.0431540173515,
130.220832358777, 104.921506533664, 49.5553381215226, 123.908567597957,
123.089699847943, 78.155161407934, 73.7757432746946, 138.299126920883,
127.115743812184, 68.7480877337669, 84.39111474778, 85.3839403789827,
128.680488220624, 70.4760196469445, 76.8936025176112, 148.413159102577,
148.413159102577, 175.960883130401, 98.5993438603892, 145.143601176911,
99.6894636984864, 141.08887439127, 123.089699847943, 129.453644576625,
111.720849360989, 103.90247060316, 118.884359339886, 151.591484239814,
101.82435207568, 99.6894636984864, 99.6894636984864, 99.6894636984864,
94.0727147457005, 152.216806827106, 104.921506533664, 65.1108537153447,
95.2307125669819, 173.879493310012, 193.411070530969, 114.475713290347,
160.642475408263, 89.4924795180001, 120.589436530402, 73.7757432746946,
127.115743812184, 118.884359339886, 34.6074177301288, 114.475713290347,
85.3839403789827, 318.342430056529, 141.77517233318, 130.982177377461,
73.7757432746946, 130.982177377461, 75.0431540173515, 154.990260276812,
104.921506533664, 73.118306923864, 123.089699847943, 157.700527725737,
81.2832532842527, 107.256062128429, 78.9816745162994, 73.7757432746946,
94.0727147457005, 99.6894636984864, 176.989650489877, 73.7757432746946,
99.6894636984864, 94.0727147457005, 95.2307125669819, 127.115743812184,
99.6894636984864, 95.6888119067371, 109.833985642199, 118.884359339886,
176.989650489877, 61.1875264339915, 81.2832532842527, 256.400649562425,
81.2832532842527, 81.2832532842527, 55.7673905084923, 75.3556793180091,
130.982177377461, 43.7351116779821, 114.475713290347, 114.475713290347,
81.2832532842527, 68.5721657022738, 109.833985642199, 97.4932940453934,
62.3983547205685, 97.4932940453934, 134.705375118879, 61.1875264339915,
118.884359339886, 65.1108537153447, 109.833985642199, 104.921506533664,
73.7757432746946, 141.77517233318, 90.4819133566401, 73.7757432746946,
134.705375118879, 109.833985642199, 91.6992261117042, 95.2307125669819,
156.503510882633, 104.921506533664, 99.6894636984864, 54.5981500331442,
32.3664131748549, 118.884359339886, 109.833985642199, 109.833985642199,
118.884359339886, 110.782650276254, 93.5572673479306, 109.833985642199,
118.884359339886, 118.884359339886, 52.1561711699147, 43.7351116779821,
117.146816914805, 118.884359339886, 148.413159102577, 101.82435207568,
94.0727147457005, 86.6951548289402, 141.77517233318, 66.9616443217359,
118.884359339886, 105.767639622758, 118.884359339886, 176.989650489877,
65.1108537153447, 85.3839403789827, 72.1504556815713, 89.2428846644714,
65.1108537153447, 145.805163374625, 87.9810327856054, 41.0998036301194,
68.3956538035455, 81.2832532842527, 73.7757432746946, 130.982177377461,
148.413159102577, 81.226746312023, 63.1888610037461, 94.0727147457005,
92.6581301668597, 76.8936025176112, 118.884359339886, 79.8549003093467,
86.6951548289402, 133.971500070695, 125.525498433836, 92.8958447894966,
72.1504556815713, 118.884359339886, 70.4760196469445, 127.115743812184,
114.475713290347, 87.9810327856054, 54.5981500331442, 84.0459576539924,
85.3839403789827, 141.77517233318, 110.782650276254, 79.8549003093467,
97.4932940453934, 171.765132627508, 137.481247831663, 75.3556793180091,
73.7757432746946, 148.413159102577, 95.2307125669819, 87.9810327856054,
109.833985642199, 81.2832532842527, 118.884359339886, 127.115743812184,
85.3839403789827, 81.2832532842527, 118.884359339886, 163.515863976238,
104.921506533664, 99.6894636984864, 281.745970551857, 138.299126920883,
176.989650489877, 136.876367635184, 186.876677628007, 90.4819133566401,
85.3839403789827, 145.143601176911, 120.589436530402, 101.82435207568,
108.874525834712, 38.6059145209601, 92.8958447894966, 126.323880709786,
191.573683548412, 155.900506146767, 87.9810327856054, 59.0970640815891,
73.7757432746946, 90.4819133566401, 118.884359339886, 151.591484239814,
272.294783220064, 97.4932940453934, 68.111994065327, 127.115743812184,
272.294783220064, 111.720849360989, 127.115743812184, 87.9810327856054,
114.475713290347, 69.7915308454408, 94.0727147457005, 127.273347006486,
160.642475408263, 171.765132627508, 141.77517233318, 115.375038556391,
72.1504556815713, 73.7757432746946, 127.115743812184, 20.9343779401018,
79.8549003093467, 102.870246933215, 115.375038556391, 97.4932940453934,
104.921506533664, 115.375038556391, 93.4866920429597, 123.089699847943,
118.884359339886, 96.3706528322308, 84.0459576539924, 127.115743812184,
95.2307125669819, 81.2832532842527, 102.870246933215, 111.720849360989,
94.0727147457005, 127.115743812184, 104.921506533664, 68.111994065327,
128.680488220624, 143.92735936583, 103.90247060316, 91.6992261117042,
95.2307125669819, 138.299126920883, 118.55677641507, 94.0727147457005,
114.475713290347, 150.01328628271, 97.4932940453934, 90.4819133566401,
81.2832532842527, 130.982177377461, 150.962783462143, 130.982177377461,
145.143601176911, 90.4819133566401, 113.567089569751, 87.9810327856054,
157.700527725737, 109.833985642199, 84.0459576539924, 160.643638171791,
47.9055670370271, 59.0970640815891, 81.2832532842527, 87.9810327856054,
87.9810327856054, 143.808556824369, 110.782650276254, 118.884359339886,
120.589436530402, 272.294783220064, 105.375892660271, 69.0108766220885,
87.9810327856054, 89.2428846644714, 171.765132627508, 133.232350826567,
200.543262324662, 92.8958447894966, 148.413159102577, 119.742556289908,
82.6796372966372, 105.927810775063, 267.901086855275, 104.921506533664,
127.115743812184, 157.700527725737, 148.413159102577, 65.1108537153447,
104.921506533664, 78.3924668009837, 139.213788138664, 112.6488963421,
79.8549003093467, 65.1108537153447, 111.720849360989, 104.921506533664,
176.989650489877, 99.6894636984864, 104.921506533664, 70.8150386167557,
94.0727147457005, 127.115743812184, 97.4932940453934, 109.833985642199,
75.1996281485946, 65.1108537153447, 146.462838515063, 81.2832532842527,
127.115743812184, 73.7757432746946, 65.1108537153447, 99.6894636984864,
79.8549003093467, 73.7757432746946, 87.9810327856054, 118.884359339886,
284.524740902214, 118.884359339886, 106.921814091211, 100.013486924706,
108.874525834712, 114.475713290347, 115.375038556391, 130.982177377461,
79.8549003093467, 106.921814091211, 22.9624006796429, 115.182455126581,
103.90247060316, 184.469464222499, 314.32933644529, 99.6894636984864,
128.680488220624, 87.9810327856054, 54.5981500331442, 99.6894636984864,
118.884359339886, 92.1611220323496, 127.115743812184, 41.7049904993395,
109.833985642199, 82.6796372966372, 277.79948020565, 99.6894636984864,
176.989650489877, 84.0459576539924, 163.515863976238, 81.2832532842527,
130.982177377461, 127.115743812184, 72.1504556815713, 104.921506533664,
186.876677628007, 176.989650489877, 139.703071527658, 73.7757432746946,
66.2294082943902, 81.2832532842527, 104.921506533664, 109.833985642199,
154.68529299563, 106.921814091211, 277.79948020565, 87.9810327856054,
90.4819133566401, 106.921814091211, 56.9055391446732, 81.2832532842527,
107.903923866876, 196.125272594255, 91.6992261117042, 200.543262324662,
32.3664131748549, 104.921506533664, 100.764272842107, 85.3839403789827,
82.6796372966372, 97.4932940453934, 121.010826565755, 133.971500070695,
148.413159102577, 104.921506533664, 81.2832532842527, 90.4819133566401,
106.921814091211, 114.475713290347, 53.3953468905727, 87.9810327856054,
66.2294082943902, 96.3706528322308, 272.403225755586, 97.1582750436503,
200.543262324662, 123.089699847943, 99.6894636984864, 94.0727147457005,
81.2832532842527, 154.68529299563, 109.833985642199, 123.089699847943,
73.7757432746946, 107.903923866876, 73.7757432746946, 87.9810327856054,
27.1405792079024, 94.0727147457005, 102.453557578372, 272.294783220064,
94.0727147457005, 81.2832532842527, 87.9810327856054, 109.833985642199,
80.4302079458208, 148.413159102577, 148.413159102577, 81.2832532842527,
118.884359339886, 80.487444359511, 91.6992261117042, 109.355626400253,
104.921506533664, 65.1108537153447, 76.8936025176112, 99.6894636984864,
99.6894636984864, 157.700527725737, 49.5553381215226, 90.4819133566401,
111.720849360989, 148.413159102577, 133.232350826567, 134.705375118879,
118.884359339886, 87.9810327856054, 109.833985642199, 130.982177377461,
89.2428846644714, 102.870246933215, 76.8936025176112, 149.695042068453,
102.870246933215, 90.4819133566401, 69.9291272064087, 106.921814091211,
87.9810327856054, 109.833985642199, 151.591484239814, 148.413159102577,
66.9616443217359, 103.90247060316, 99.6894636984864, 134.705375118879,
43.7351116779821, 116.707154559148, 141.77517233318, 144.47808766541,
134.705375118879, 118.884359339886, 189.244162932803, 160.642475408263,
96.0316570194457, 111.065194319742, 148.413159102577, 99.6894636984864,
81.2832532842527, 118.884359339886, 127.115743812184, 104.921506533664,
112.6488963421, 281.745970551857, 114.477521132502, 111.720849360989,
134.705375118879, 75.3556793180091, 119.74085129618, 118.884359339886,
27.1405792079024, 182.020620963512, 65.1108537153447, 141.77517233318,
114.475713290347, 135.59805975224, 117.146816914805, 99.6894636984864,
277.79948020565, 94.0727147457005, 94.0727147457005, 96.3706528322308,
75.3556793180091, 104.921506533664, 95.2307125669819, 54.5981500331442,
89.2428846644714, 145.143601176911, 96.8667032433329, 130.982177377461,
84.0459576539924, 148.155046727428, 49.5553381215226, 141.77517233318,
115.375038556391, 65.1108537153447, 85.3839403789827, 101.82435207568,
95.2307125669819, 99.6894636984864, 147.116689159978, 140.398200161944,
109.833985642199, 127.115743812184, 115.375038556391, 59.0970640815891,
76.8936025176112, 141.77517233318, 70.9095858696657, 200.543262324662,
97.4932940453934, 99.6894636984864, 186.876677628007, 81.2832532842527,
160.642475408263, 63.9666908300607, 87.9810327856054, 73.7757432746946,
145.143601176911, 141.77517233318, 76.8936025176112, 123.908567597957,
118.884359339886, 279.1974969917, 54.5981500331442, 154.68529299563,
180.030197076236, 118.884359339886, 87.9810327856054, 91.6992261117042,
59.0970640815891, 123.908567597957, 109.833985642199, 94.0727147457005,
117.146816914805, 65.1108537153447, 97.4932940453934, 118.019753340006,
141.77517233318, 116.176695171054, 75.3556793180091, 95.2307125669819,
111.720849360989, 103.90247060316, 147.116689159978, 87.9810327856054,
118.884359339886, 100.764272842107, 120.589436530402, 193.866878146586,
118.884359339886, 267.901086855275, 174.402876012275, 133.971500070695,
154.68529299563, 154.68529299563, 118.884359339886, 95.2307125669819,
109.833985642199, 111.720849360989, 134.705375118879, 85.9010443226556,
118.884359339886, 87.9810327856054, 92.8958447894966, 109.833985642199,
124.720446242886, 127.901232979118, 97.4932940453934, 198.350290464385,
92.8958447894966, 94.0727147457005, 148.413159102577, 79.8549003093467,
118.884359339886, 87.9810327856054, 94.0727147457005, 150.070476909424,
176.989650489877, 73.7757432746946, 125.12381572196, 277.79948020565,
107.903923866876, 60.7397785177603, 92.0363233010764, 109.833985642199,
66.9616443217359, 114.475713290347, 170.6951277113, 130.982177377461,
127.115743812184, 117.146816914805, 123.089699847943, 120.589436530402,
130.982177377461, 81.2832532842527, 36.6737041960799, 104.921506533664,
74.412963512917, 129.067820506745, 84.1000101164483, 185.918662879754,
105.927810775063, 163.515863976238, 63.1888610037461, 134.705375118879,
120.926701544426, 94.0727147457005, 132.487821126123, 133.232350826567,
145.143601176911, 118.884359339886, 150.330653495766, 278.964466695847,
98.2692308087927, 99.6894636984864, 94.0727147457005, 176.989650489877,
166.324941233877, 94.0727147457005, 139.703071527658, 78.3924668009837,
141.775856466307, 116.265324062503, 148.413159102577, 87.9810327856054,
90.4819133566401, 141.77517233318, 63.0927167348386, 65.1108537153447,
151.591484239814, 123.089699847943, 118.884359339886, 79.8549003093467,
104.004959912222, 55.5361044177385, 99.6894636984864, 99.6894636984864,
193.452606894109, 102.870246933215, 164.646988930328, 69.6190369046763,
99.6894636984864, 114.475713290347, 99.6894636984864, 95.2307125669819,
82.6796372966372, 116.943081320617, 54.5981500331442, 138.299126920883,
130.982177377461, 281.745970551857, 163.515863976238, 179.214858036271,
126.323880709786, 70.4760196469445, 118.019753340006, 71.3196387942183,
114.475713290347, 138.299126920883, 54.5981500331442, 94.0727147457005,
80.4302079458208, 89.2428846644714, 117.146816914805, 127.115743812184,
81.2832532842527, 52.4070640482002, 99.6894636984864, 63.1888610037461,
196.125272594255, 101.82435207568, 107.903923866876, 51.2396549228382,
99.6894636984864, 63.1888610037461, 148.413159102577, 99.6894636984864,
130.982177377461, 101.82435207568, 99.6894636984864, 79.8549003093467,
160.642475408263, 134.705375118879, 160.642475408263, 91.6992261117042,
75.3556793180091, 136.15771083723, 134.705375118879, 81.9853178344996,
176.989650489877, 118.886080351034, 121.262745732618, 94.0727147457005,
99.6894636984864, 123.908567597957, 118.884359339886, 82.6796372966372,
81.2832532842527, 104.921506533664, 99.6894636984864, 136.15771083723,
81.2832532842527, 68.5086896572248, 81.2832532842527, 114.475713290347,
118.019753340006, 99.6894636984864, 118.884359339886, 118.884359339886,
123.089699847943, 109.833985642199, 102.870246933215, 163.515863976238,
117.146816914805, 92.3000614282589, 100.013486924706, 55.0697284222779,
101.82435207568, 148.413159102577, 115.375038556391, 73.7757432746946,
267.901086855275, 129.453644576625, 96.3706528322308, 87.9810327856054,
104.921506533664, 130.982177377461, 190.547678564741, 118.019753340006,
79.8549003093467, 72.4793162971252, 148.413159102577, 74.4446382014083,
118.884359339886, 128.680488220624, 87.9810327856054, 267.901086855275,
104.921506533664, 92.8958447894966, 70.4760196469445, 200.543262324662,
102.870246933215, 102.870246933215, 65.1108537153447, 109.833985642199,
127.115743812184, 94.0727147457005, 67.8625234113681, 125.525498433836,
165.208773767255, 81.2832532842527, 112.6488963421, 76.8936025176112,
75.0431540173515, 128.680488220624, 59.0970640815891, 63.7733911962488,
84.0459576539924, 93.9371815810521, 82.6796372966372, 118.884359339886,
81.2832532842527, 66.9616443217359, 166.324941233877, 99.6894636984864,
141.77517233318, 143.134940811134, 35.0334213214995, 109.833985642199,
111.014405508798, 132.487821126123, 148.413159102577, 73.7757432746946,
143.699710133813, 99.6894636984864, 272.294783220064, 200.543262324662,
85.3865890236244, 109.833985642199, 61.0524210501246, 118.884359339886,
145.805163374625, 112.6488963421, 106.921814091211, 106.921814091211,
127.115743812184, 87.9810327856054, 144.47808766541, 160.409701962289,
87.9810327856054, 65.1108537153447, 176.989650489877, 54.5981500331442,
109.833985642199, 81.698421724729, 114.475713290347, 142.634472767972,
160.642475408263, 94.0727147457005, 54.5981500331442, 73.7757432746946,
160.642475408263, 148.413159102577, 87.9823064294987, 101.82435207568,
111.720849360989, 106.921814091211, 126.323880709786, 109.833985642199,
92.8958447894966, 54.5981500331442, 96.3706528322308, 114.475713290347,
94.0727147457005, 95.2307125669819, 139.00340805947, 81.2832532842527,
94.0727147457005, 87.9810327856054, 138.299126920883)), row.names = c(NA,
-1000L), class = c("tbl_df", "tbl", "data.frame"))
这是我在25、40和60 时使用结的函数
Call:
lm(formula = wage ~ bs(age, knots = c(25, 40, 60)), data = salary)
Coefficients:
(Intercept) bs(age, knots = c(25, 40, 60))1
56.423 8.631
bs(age, knots = c(25, 40, 60))2 bs(age, knots = c(25, 40, 60))3
46.314 69.927
bs(age, knots = c(25, 40, 60))4 bs(age, knots = c(25, 40, 60))5
65.584 29.341
bs(age, knots = c(25, 40, 60))6
27.058
然后我计算了y-hat和sqd误差
y_hat = (56.423+8.631*salary$age+46.314*(salary$age)^2+69.927*(salary$age)^3+65.584*(salary$age-25)^3+29.341*(salary$age-40)^3+27.058*(salary$age-60)^3)
sqd_error = (salary$wage - y_hat)^2
sqd_error
这是我得到的(部分原因是它是一个大数据集,你知道的(
[1] 3.673488e+12 1.514171e+11 4.765440e+13 3.476206e+13 9.769555e+13
[6] 1.643685e+14 4.079991e+13 1.388170e+12 2.480692e+13 1.273657e+14
[11] 4.765459e+13 5.607993e+12 7.259031e+12 1.720632e+13 1.643683e+14
[16] 1.116993e+14 1.148553e+13 9.769283e+13 2.101982e+14 1.148543e+13
[21] 1.413636e+13 2.074496e+13 1.545190e+15 2.074396e+13 1.413716e+13
[26] 8.519755e+13 3.476289e+13 5.608069e+12 2.369560e+14 3.673471e+12
[31] 1.860756e+14 1.116990e+14 4.221360e+12 5.607848e+12 9.206242e+12
[36] 2.101994e+14 9.615010e+14 1.888253e+10 3.071311e+12 1.399047e+11
[41] 4.045521e+11 1.398721e+11 3.476335e+13 9.769558e+13 1.720677e+13
[46] 1.273651e+14 7.258070e+12 2.369541e+14 1.887217e+10 4.221217e+12
您的预测函数不使用与拟合函数相同的基,您只是使用多项式基。您应该在R中使用predict()
函数,例如
fit <- lm(wage ~ bs(age, knots = c(25, 40, 60)), data = salary)
predict(fit)
如果你想在新的年龄进行预测,请使用
predict(fit, newdata = data.frame(age = 50:60))