我想在我的数据中使用以下函数:
f(x)=偏移+幅度 sin(频率 T +相位),
或根据Wikipedia:f(x)= C + alpha sin(ω T + phi)
我的数据存储在两列的文件中,我通过以下方式导入它们:
data<-read.table("C:/PATH/data.txt", header = FALSE, sep = "\t")
并按以下方式转换它们:
minV<-data[3]
cV<-data[1]
values<-as.numeric(unlist(minV))
T<-as.numeric(unlist(cV))
因此,我得到两个类型为double的变量,一个名为“T”(相当于上面等式中的时间T),另一个称为“值”(相当于上面等式中函数f(x)的值) 。 到目前为止,一切都很好! 但是,当我想将f(x)拟合到我的数据时,我会得到不好的结果(如果有的话)。这是我的代码:
r<-nls(values~C+alpha*sin(W*T+phi), start=list(C=8958.34, alpha=115.886, W=0.0652, phi=14.9286),
我现在的问题是,在这个简单的四行示例(至少据我所知)中唯一可能出错的是起始值。然而,它们是Gnuplot拟合的结果(它使用最小二乘法)并且它们看起来很好(因为我没有“10声望”我无法附加情节,sry)。 {edit1:plot attached} 上面代码的结果:
C: 8950.06571617 alpha: -23.58439668 W: 0.06416806 phi: 16.46585060
我接下来的步骤是改变起始值以获得合理的结果(也许起始值太高了......) - 没有成功! 所以现在我的问题是:问题出在哪里?我想使用R,因为我希望,我可以产生一个拟合,这是a)更好的b)更糟糕的起始值比Gnuplot(至少b)对我来说是一个非常重要的标准,这就是我不想要的原因使用Gnuplot)。
Ps。:是的,这是我使用R的第一天。所以,如果你有一个解决方案/理由,那么如果我能理解它就会很好。
PPs:对于那些想要获取相关信息的人:我收集了很多链接,所以如果我获得至少“10个声望”,我可以在这里发布。 {edit2:links attached}
[R] fitting periodic 'sine wave' model
Nonlinear Regression and Nonlinear Least Squares
Nonlinear Regression and Nonlinear Least Squares in R
Nichtlineare Regression (German)
How to fit a curve to a sinusoidal wave
official Documentation -> Manuals
Link Roland provided in his answer
PPs:这里使用了我使用的数据(由tabstop分隔,只有第一列(“计数器”)和三个(“最小值”);由于最大数字字符/我删除了最后一列的一些值发布{edit3:删除附加条目,因为附加的链接}} {edit4:从“第二列(”角度“)”更改为“第一列(”计数器“)”。在代码和答案中它的第一列也是。}:
#Counter angle min value EV-min value max value EV_max-value
1 0,703125 9033 -81,990234375 9043 -82,556640625
2 1,40625 9033 -81,990234375 9042 -81,556640625
3 2,109375 9027 -75,990234375 9042 -81,556640625
4 2,8125 9025 -73,990234375 9038 -77,556640625
5 3,515625 9018 -66,990234375 9031 -70,556640625
6 4,21875 9013 -61,990234375 9025 -64,556640625
7 4,921875 9009 -57,990234375 9020 -59,556640625
8 5,625 9011 -59,990234375 9017 -56,556640625
9 6,328125 9000 -48,990234375 9009 -48,556640625
10 7,03125 8996 -44,990234375 9004 -43,556640625
11 7,734375 8989 -37,990234375 9004 -43,556640625
12 8,4375 8983 -31,990234375 8993 -32,556640625
13 9,140625 8983 -31,990234375 8991 -30,556640625
14 9,84375 8982 -30,990234375 8991 -30,556640625
15 10,546875 8975 -23,990234375 8983 -22,556640625
16 11,25 8964 -12,990234375 8978 -17,556640625
17 11,953125 8967 -15,990234375 8977 -16,556640625
18 12,65625 8958 -6,990234375 8967 -6,556640625
19 13,359375 8951 0,009765625 8960 0,443359375
20 14,0625 8947 4,009765625 8954 6,443359375
21 14,765625 8938 13,009765625 8951 9,443359375
22 15,46875 8933 18,009765625 8940 20,443359375
23 16,171875 8929 22,009765625 8941 19,443359375
24 16,875 8925 26,009765625 8933 27,443359375
25 17,578125 8923 28,009765625 8929 31,443359375
26 18,28125 8920 31,009765625 8927 33,443359375
27 18,984375 8912 39,009765625 8921 39,443359375
28 19,6875 8899 52,009765625 8916 44,443359375
29 20,390625 8899 52,009765625 8907 53,443359375
30 21,09375 8894 57,009765625 8901 59,443359375
31 21,796875 8892 59,009765625 8896 64,443359375
32 22,5 8886 65,009765625 8896 64,443359375
33 23,203125 8881 70,009765625 8892 68,443359375
34 23,90625 8879 72,009765625 8886 74,443359375
35 24,609375 8884 67,009765625 8888 72,443359375
36 25,3125 8875 76,009765625 8886 74,443359375
37 26,015625 8864 87,009765625 8881 79,443359375
38 26,71875 8860 91,009765625 8870 90,443359375
39 27,421875 8864 87,009765625 8872 88,443359375
40 28,125 8862 89,009765625 8872 88,443359375
41 28,828125 8859 92,009765625 8868 92,443359375
42 29,53125 8857 94,009765625 8864 96,443359375
43 30,234375 8852 99,009765625 8860 100,443359375
44 30,9375 8852 99,009765625 8855 105,443359375
45 31,640625 8847 104,009765625 8857 103,443359375
46 32,34375 8844 107,009765625 8853 107,443359375
47 33,046875 8842 109,009765625 8849 111,443359375
48 33,75 8844 107,009765625 8855 105,443359375
49 34,453125 8846 105,009765625 8852 108,443359375
50 35,15625 8846 105,009765625 8853 107,443359375
51 35,859375 8839 112,009765625 8849 111,443359375
52 36,5625 8841 110,009765625 8849 111,443359375
53 37,265625 8841 110,009765625 8846 114,443359375
54 37,96875 8844 107,009765625 8852 108,443359375
55 38,671875 8841 110,009765625 8852 108,443359375
56 39,375 8835 116,009765625 8846 114,443359375
57 40,078125 8839 112,009765625 8847 113,443359375
58 40,78125 8839 112,009765625 8847 113,443359375
59 41,484375 8846 105,009765625 8857 103,443359375
60 42,1875 8849 102,009765625 8860 100,443359375
61 42,890625 8852 99,009765625 8860 100,443359375
62 43,59375 8852 99,009765625 8860 100,443359375
63 44,296875 8853 98,009765625 8866 94,443359375
64 45 8857 94,009765625 8864 96,443359375
65 45,703125 8860 91,009765625 8864 96,443359375
66 46,40625 8860 91,009765625 8870 90,443359375
67 47,109375 8866 85,009765625 8875 85,443359375
68 47,8125 8868 83,009765625 8878 82,443359375
69 48,515625 8875 76,009765625 8884 76,443359375
70 49,21875 8884 67,009765625 8892 68,443359375
71 49,921875 8883 68,009765625 8894 66,443359375
72 50,625 8888 63,009765625 8897 63,443359375
73 51,328125 8892 59,009765625 8899 61,443359375
74 52,03125 8897 54,009765625 8909 51,443359375
75 52,734375 8903 48,009765625 8910 50,443359375
76 53,4375 8910 41,009765625 8921 39,443359375
77 54,140625 8916 35,009765625 8923 37,443359375
78 54,84375 8923 28,009765625 8929 31,443359375
79 55,546875 8925 26,009765625 8936 24,443359375
80 56,25 8930 21,009765625 8938 22,443359375
81 56,953125 8929 22,009765625 8938 22,443359375
82 57,65625 8936 15,009765625 8947 13,443359375
83 58,359375 8943 8,009765625 8954 6,443359375
84 59,0625 8947 4,009765625 8960 0,443359375
85 59,765625 8960 -8,990234375 8965 -4,556640625
86 60,46875 8965 -13,990234375 8973 -12,556640625
87 61,171875 8964 -12,990234375 8973 -12,556640625
88 61,875 8969 -17,990234375 8980 -19,556640625
89 62,578125 8967 -15,990234375 8980 -19,556640625
90 63,28125 8969 -17,990234375 8975 -14,556640625
91 63,984375 8975 -23,990234375 8983 -22,556640625
92 64,6875 8978 -26,990234375 8991 -30,556640625
93 65,390625 8985 -33,990234375 8998 -37,556640625
94 66,09375 8991 -39,990234375 9000 -39,556640625
95 66,796875 9000 -48,990234375 9006 -45,556640625
96 67,5 9004 -52,990234375 9013 -52,556640625
97 68,203125 9009 -57,990234375 9017 -56,556640625
98 68,90625 9013 -61,990234375 9022 -61,556640625
99 69,609375 9017 -65,990234375 9027 -66,556640625
100 70,3125 9024 -72,990234375 9033 -72,556640625
101 71,015625 9029 -77,990234375 9037 -76,556640625
102 71,71875 9031 -79,990234375 9042 -81,556640625
103 72,421875 9035 -83,990234375 9043 -82,556640625
104 73,125 9040 -88,990234375 9048 -87,556640625
105 73,828125 9048 -96,990234375 9054 -93,556640625
106 74,53125 9049 -97,990234375 9060 -99,556640625
107 75,234375 9051 -99,990234375 9064 -103,556640625
108 75,9375 9061 -109,990234375 9072 -111,556640625
109 76,640625 9060 -108,990234375 9069 -108,556640625
110 77,34375 9066 -114,990234375 9072 -111,556640625
111 78,046875 9064 -112,990234375 9072 -111,556640625
112 78,75 9067 -115,990234375 9071 -110,556640625
113 79,453125 9072 -120,990234375 9082 -121,556640625
114 80,15625 9069 -117,990234375 9080 -119,556640625
115 80,859375 9072 -120,990234375 9082 -121,556640625
116 81,5625 9074 -122,990234375 9083 -122,556640625
117 82,265625 9078 -126,990234375 9089 -128,556640625
118 82,96875 9082 -130,990234375 9090 -129,556640625
119 83,671875 9085 -133,990234375 9090 -129,556640625
120 84,375 9078 -126,990234375 9089 -128,556640625
121 85,078125 9072 -120,990234375 9083 -122,556640625
122 85,78125 9072 -120,990234375 9080 -119,556640625
123 86,484375 9076 -124,990234375 9085 -124,556640625
124 87,1875 9071 -119,990234375 9076 -115,556640625
125 87,890625 9067 -115,990234375 9074 -113,556640625
126 88,59375 9066 -114,990234375 9078 -117,556640625
127 89,296875 9066 -114,990234375 9078 -117,556640625
128 90 9066 -114,990234375 9076 -115,556640625
129 90,703125 9066 -114,990234375 9072 -111,556640625
130 91,40625 9061 -109,990234375 9071 -110,556640625
131 92,109375 9058 -106,990234375 9069 -108,556640625
132 92,8125 9061 -109,990234375 9069 -108,556640625
133 93,515625 9054 -102,990234375 9069 -108,556640625
134 94,21875 9053 -101,990234375 9056 -95,556640625
135 94,921875 9053 -101,990234375 9060 -99,556640625
136 95,625 9048 -96,990234375 9056 -95,556640625
137 96,328125 9042 -90,990234375 9051 -90,556640625
138 97,03125 9042 -90,990234375 9045 -84,556640625
139 97,734375 9035 -83,990234375 9043 -82,556640625
140 98,4375 9033 -81,990234375 9043 -82,556640625
141 99,140625 9024 -72,990234375 9031 -70,556640625
142 99,84375 9027 -75,990234375 9037 -76,556640625
143 100,546875 9018 -66,990234375 9031 -70,556640625
144 101,25 9011 -59,990234375 9020 -59,556640625
145 101,953125 9011 -59,990234375 9018 -57,556640625
146 102,65625 9006 -54,990234375 9011 -50,556640625
147 103,359375 9000 -48,990234375 9006 -45,556640625
148 104,0625 8993 -41,990234375 9004 -43,556640625
149 104,765625 8985 -33,990234375 8998 -37,556640625
150 105,46875 8987 -35,990234375 8994 -33,556640625
151 106,171875 8982 -30,990234375 8987 -26,556640625
152 106,875 8973 -21,990234375 8983 -22,556640625
153 107,578125 8965 -13,990234375 8977 -16,556640625
154 108,28125 8969 -17,990234375 8977 -16,556640625
155 108,984375 8965 -13,990234375 8971 -10,556640625
156 109,6875 8953 -1,990234375 8965 -4,556640625
157 110,390625 8951 0,009765625 8962 -1,556640625
158 111,09375 8943 8,009765625 8951 9,443359375
159 111,796875 8938 13,009765625 8951 9,443359375
160 112,5 8930 21,009765625 8941 19,443359375
161 113,203125 8927 24,009765625 8940 20,443359375
162 113,90625 8923 28,009765625 8930 30,443359375
163 114,609375 8923 28,009765625 8930 30,443359375
164 115,3125 8914 37,009765625 8929 31,443359375
165 116,015625 8909 42,009765625 8920 40,443359375
166 116,71875 8901 50,009765625 8914 46,443359375
167 117,421875 8899 52,009765625 8910 50,443359375
168 118,125 8890 61,009765625 8896 64,443359375
169 118,828125 8886 65,009765625 8896 64,443359375
170 119,53125 8884 67,009765625 8896 64,443359375
171 120,234375 8881 70,009765625 8890 70,443359375
172 120,9375 8883 68,009765625 8890 70,443359375
173 121,640625 8878 73,009765625 8886 74,443359375
174 122,34375 8875 76,009765625 8883 77,443359375
175 123,046875 8862 89,009765625 8875 85,443359375
176 123,75 8866 85,009765625 8878 82,443359375
177 124,453125 8866 85,009765625 8875 85,443359375
178 125,15625 8864 87,009765625 8875 85,443359375
179 125,859375 8860 91,009765625 8868 92,443359375
180 126,5625 8857 94,009765625 8870 90,443359375
181 127,265625 8853 98,009765625 8857 103,443359375
182 127,96875 8847 104,009765625 8859 101,443359375
183 128,671875 8847 104,009765625 8857 103,443359375
184 129,375 8844 107,009765625 8849 111,443359375
185 130,078125 8844 107,009765625 8855 105,443359375
186 130,78125 8844 107,009765625 8853 107,443359375
187 131,484375 8841 110,009765625 8852 108,443359375
188 132,1875 8839 112,009765625 8849 111,443359375
189 132,890625 8841 110,009765625 8849 111,443359375
190 133,59375 8841 110,009765625 8847 113,443359375
191 134,296875 8841 110,009765625 8846 114,443359375
192 135 8839 112,009765625 8847 113,443359375
193 135,703125 8836 115,009765625 8844 116,443359375
194 136,40625 8836 115,009765625 8844 116,443359375
195 137,109375 8841 110,009765625 8849 111,443359375
196 137,8125 8844 107,009765625 8855 105,443359375
197 138,515625 8844 107,009765625 8857 103,443359375
198 139,21875 8853 98,009765625 8862 98,443359375
199 139,921875 8852 99,009765625 8860 100,443359375
200 140,625 8853 98,009765625 8862 98,443359375
201 141,328125 8855 96,009765625 8864 96,443359375
202 142,03125 8859 92,009765625 8862 98,443359375
203 142,734375 8859 92,009765625 8866 94,443359375
204 143,4375 8868 83,009765625 8878 82,443359375
205 144,140625 8870 81,009765625 8883 77,443359375
206 144,84375 8875 76,009765625 8884 76,443359375
207 145,546875 8881 70,009765625 8890 70,443359375
208 146,25 8886 65,009765625 8894 66,443359375
209 146,953125 8888 63,009765625 8897 63,443359375
210 147,65625 8890 61,009765625 8903 57,443359375
211 148,359375 8903 48,009765625 8910 50,443359375
212 149,0625 8903 48,009765625 8916 44,443359375
213 149,765625 8907 44,009765625 8920 40,443359375
214 150,46875 8918 33,009765625 8925 35,443359375
215 151,171875 8921 30,009765625 8929 31,443359375
216 151,875 8927 24,009765625 8936 24,443359375
217 152,578125 8929 22,009765625 8940 20,443359375
218 153,28125 8933 18,009765625 8943 17,443359375
219 153,984375 8940 11,009765625 8947 13,443359375
220 154,6875 8943 8,009765625 8954 6,443359375
221 155,390625 8954 -2,990234375 8964 -3,556640625
222 156,09375 8956 -4,990234375 8967 -6,556640625
223 156,796875 8960 -8,990234375 8980 -19,556640625
224 157,5 8971 -19,990234375 8977 -16,556640625
225 158,203125 8973 -21,990234375 8980 -19,556640625
226 158,90625 8973 -21,990234375 8983 -22,556640625
227 159,609375 8969 -17,990234375 8980 -19,556640625
228 160,3125 8971 -19,990234375 8987 -26,556640625
229 161,015625 8978 -26,990234375 8989 -28,556640625
230 161,71875 8987 -35,990234375 8998 -37,556640625
231 162,421875 8994 -42,990234375 9004 -43,556640625
232 163,125 9000 -48,990234375 9006 -45,556640625
233 163,828125 9006 -54,990234375 9013 -52,556640625
234 164,53125 9009 -57,990234375 9020 -59,556640625
235 165,234375 9014 -62,990234375 9024 -63,556640625
236 165,9375 9020 -68,990234375 9029 -68,556640625
237 166,640625 9025 -73,990234375 9035 -74,556640625
238 167,34375 9027 -75,990234375 9038 -77,556640625
239 168,046875 9033 -81,990234375 9042 -81,556640625
240 168,75 9033 -81,990234375 9043 -82,556640625
241 169,453125 9040 -88,990234375 9049 -88,556640625
242 170,15625 9045 -93,990234375 9054 -93,556640625
243 170,859375 9048 -96,990234375 9060 -99,556640625
244 171,5625 9056 -104,990234375 9064 -103,556640625
245 172,265625 9058 -106,990234375 9067 -106,556640625
246 172,96875 9060 -108,990234375 9071 -110,556640625
247 173,671875 9061 -109,990234375 9071 -110,556640625
248 174,375 9067 -115,990234375 9076 -115,556640625
249 175,078125 9067 -115,990234375 9078 -117,556640625
250 175,78125 9067 -115,990234375 9078 -117,556640625
251 176,484375 9071 -119,990234375 9082 -121,556640625
252 177,1875 9069 -117,990234375 9082 -121,556640625
253 177,890625 9072 -120,990234375 9085 -124,556640625
254 178,59375 9082 -130,990234375 9089 -128,556640625
255 179,296875 9080 -128,990234375 9092 -131,556640625
256 180 9080 -128,990234375 9092 -131,556640625
257 180,703125 9078 -126,990234375 9089 -128,556640625
258 181,40625 9076 -124,990234375 9083 -122,556640625
259 182,109375 9074 -122,990234375 9085 -124,556640625
260 182,8125 9076 -124,990234375 9082 -121,556640625
261 183,515625 9071 -119,990234375 9078 -117,556640625
262 184,21875 9067 -115,990234375 9078 -117,556640625
263 184,921875 9071 -119,990234375 9078 -117,556640625
264 185,625 9067 -115,990234375 9076 -115,556640625
265 186,328125 9069 -117,990234375 9078 -117,556640625
266 187,03125 9064 -112,990234375 9074 -113,556640625
267 187,734375 9064 -112,990234375 9074 -113,556640625
268 188,4375 9061 -109,990234375 9071 -110,556640625
269 189,140625 9058 -106,990234375 9066 -105,556640625
270 189,84375 9056 -104,990234375 9066 -105,556640625
271 190,546875 9053 -101,990234375 9056 -95,556640625
272 191,25 9051 -99,990234375 9061 -100,556640625
273 191,953125 9048 -96,990234375 9058 -97,556640625
274 192,65625 9040 -88,990234375 9054 -93,556640625
275 193,359375 9042 -90,990234375 9048 -87,556640625
276 194,0625 9037 -85,990234375 9045 -84,556640625
277 194,765625 9033 -81,990234375 9045 -84,556640625
278 195,46875 9029 -77,990234375 9043 -82,556640625
279 196,171875 9027 -75,990234375 9040 -79,556640625
280 196,875 9020 -68,990234375 9038 -77,556640625
281 197,578125 9011 -59,990234375 9029 -68,556640625
282 198,28125 9009 -57,990234375 9020 -59,556640625
283 198,984375 9002 -50,990234375 9013 -52,556640625
284 199,6875 8996 -44,990234375 9009 -48,556640625
285 200,390625 8993 -41,990234375 9004 -43,556640625
286 201,09375 8989 -37,990234375 8996 -35,556640625
287 201,796875 8983 -31,990234375 8991 -30,556640625
288 202,5 8985 -33,990234375 8991 -30,556640625
289 203,203125 8978 -26,990234375 8989 -28,556640625
290 203,90625 8977 -25,990234375 8983 -22,556640625
291 204,609375 8971 -19,990234375 8982 -21,556640625
292 205,3125 8958 -6,990234375 8971 -10,556640625
293 206,015625 8958 -6,990234375 8965 -4,556640625
294 206,71875 8947 4,009765625 8958 2,443359375
295 207,421875 8941 10,009765625 8954 6,443359375
296 208,125 8934 17,009765625 8951 9,443359375
297 208,828125 8933 18,009765625 8940 20,443359375
298 209,53125 8929 22,009765625 8938 22,443359375
299 210,234375 8927 24,009765625 8934 26,443359375
300 210,9375 8920 31,009765625 8930 30,443359375
301 211,640625 8916 35,009765625 8921 39,443359375
302 212,34375 8909 42,009765625 8921 39,443359375
303 213,046875 8903 48,009765625 8910 50,443359375
304 213,75 8894 57,009765625 8905 55,443359375
305 214,453125 8894 57,009765625 8899 61,443359375
306 215,15625 8883 68,009765625 8897 63,443359375
307 215,859375 8881 70,009765625 8890 70,443359375
308 216,5625 8886 65,009765625 8894 66,443359375
309 217,265625 8879 72,009765625 8888 72,443359375
310 217,96875 8878 73,009765625 8886 74,443359375
311 218,671875 8873 78,009765625 8883 77,443359375
312 219,375 8862 89,009765625 8878 82,443359375
313 220,078125 8866 85,009765625 8878 82,443359375
314 220,78125 8862 89,009765625 8870 90,443359375
315 221,484375 8857 94,009765625 8862 98,443359375
316 222,1875 8859 92,009765625 8864 96,443359375
317 222,890625 8859 92,009765625 8870 90,443359375
318 223,59375 8857 94,009765625 8868 92,443359375
319 224,296875 8852 99,009765625 8860 100,443359375
320 225 8847 104,009765625 8857 103,443359375
321 225,703125 8842 109,009765625 8853 107,443359375
322 226,40625 8844 107,009765625 8855
323 227,109375 8849 102,009765625 8857
324 227,8125 8839 112,009765625 8849
325 228,515625 8842 109,009765625 8849
326 229,21875 8841 110,009765625 8849
327 229,921875 8842 109,009765625 8852
328 230,625 8836 115,009765625 8855
329 231,328125 8839 112,009765625 8846
330 232,03125 8836 115,009765625 8844
331 232,734375 8839 112,009765625 8846
332 233,4375 8842 109,009765625 8852
333 234,140625 8846 105,009765625 8857
334 234,84375 8852 99,009765625 8857
335 235,546875 8857 94,009765625 8862
336 236,25 8855 96,009765625 8862
337 236,953125 8857 94,009765625 8864
338 237,65625 8857 94,009765625 8864
339 238,359375 8857 94,009765625 8866
340 239,0625 8857 94,009765625 8872
341 239,765625 8866 85,009765625 8878
342 240,46875 8873 78,009765625 8881
343 241,171875 8878 73,009765625 8886
344 241,875 8883 68,009765625 8892
345 242,578125 8886 65,009765625 8896
346 243,28125 8892 59,009765625 8899
347 243,984375 8896 55,009765625 8905
348 244,6875 8903 48,009765625 8909
349 245,390625 8907 44,009765625 8916
350 246,09375 8912 39,009765625 8920
351 246,796875 8914 37,009765625 8929
352 247,5 8927 24,009765625 8934
353 248,203125 8925 26,009765625 8936
354 248,90625 8929 22,009765625 8938
355 249,609375 8933 18,009765625 8940
356 250,3125 8940 11,009765625 8951
357 251,015625 8949 2,009765625 8958
358 251,71875 8958 -6,990234375 8964
359 252,421875 8962 -10,990234375 8973
360 253,125 8965 -13,990234375 8973
361 253,828125 8967 -15,990234375 8980
362 254,53125 8971 -19,990234375 8980
363 255,234375 8967 -15,990234375 8982
364 255,9375 8975 -23,990234375 8982
365 256,640625 8975 -23,990234375 8983
366 257,34375 8977 -25,990234375 8991
367 258,046875 8989 -37,990234375 8994
368 258,75 8989 -37,990234375 9000
369 259,453125 8996 -44,990234375 9007
370 260,15625 9004 -52,990234375 9013
371 260,859375 9007 -55,990234375 9018
372 261,5625 9014 -62,990234375 9022
373 262,265625 9017 -65,990234375 9025
374 262,96875 9025 -73,990234375 9037
375 263,671875 9029 -77,990234375 9042
376 264,375 9033 -81,990234375 9042
377 265,078125 9031 -79,990234375 9043
378 265,78125 9042 -90,990234375 9051
379 266,484375 9043 -91,990234375 9054
380 267,1875 9048 -96,990234375 9058
381 267,890625 9048 -96,990234375 9061
382 268,59375 9056 -104,990234375 9060
383 269,296875 9060 -108,990234375 9067
384 270 9064 -112,990234375 9071
385 270,703125 9064 -112,990234375 9072
386 271,40625 9069 -117,990234375 9074
387 272,109375 9071 -119,990234375 9080
388 272,8125 9072 -120,990234375 9082
389 273,515625 9071 -119,990234375 9083
390 274,21875 9078 -126,990234375 9083
391 274,921875 9080 -128,990234375 9087
392 275,625 9082 -130,990234375 9085
393 276,328125 9083 -131,990234375 9090
394 277,03125 9072 -120,990234375 9087
395 277,734375 9071 -119,990234375 9078
396 278,4375 9072 -120,990234375 9082
397 279,140625 9076 -124,990234375 9085
398 279,84375 9074 -122,990234375 9082
399 280,546875 9069 -117,990234375 9074
400 281,25 9066 -114,990234375 9076
401 281,953125 9069 -117,990234375 9076
402 282,65625 9064 -112,990234375 9072
403 283,359375 9066 -114,990234375 9074
404 284,0625 9064 -112,990234375 9069
405 284,765625 9064 -112,990234375 9071
406 285,46875 9056 -104,990234375 9067
407 286,171875 9056 -104,990234375 9066
408 286,875 9056 -104,990234375 9061
409 287,578125 9051 -99,990234375 9060
410 288,28125 9042 -90,990234375 9058
411 288,984375 9040 -88,990234375 9051
412 289,6875 9038 -86,990234375 9048
413 290,390625 9035 -83,990234375 9043
414 291,09375 9033 -81,990234375 9042
415 291,796875 9027 -75,990234375 9038
416 292,5 9027 -75,990234375 9038
417 293,203125 9020 -68,990234375 9029
418 293,90625 9013 -61,990234375 9022
419 294,609375 9011 -59,990234375 9024
420 295,3125 9004 -52,990234375 9013
421 296,015625 9000 -48,990234375 9004
422 296,71875 8994 -42,990234375 9004
423 297,421875 8983 -31,990234375 8998
424 298,125 8978 -26,990234375 8989
425 298,828125 8982 -30,990234375 8989
426 299,53125 8978 -26,990234375 8983
427 300,234375 8973 -21,990234375 8982
428 300,9375 8969 -17,990234375 8982
429 301,640625 8960 -8,990234375 8975
430 302,34375 8953 -1,990234375 8965
431 303,046875 8949 2,009765625 8954
432 303,75 8947 4,009765625 8954
433 304,453125 8936 15,009765625 8949
434 305,15625 8930 21,009765625 8941
435 305,859375 8925 26,009765625 8936
436 306,5625 8925 26,009765625 8934
437 307,265625 8918 33,009765625 8927
438 307,96875 8916 35,009765625 8921
439 308,671875 8905 46,009765625 8920
440 309,375 8899 52,009765625 8910
441 310,078125 8892 59,009765625 8905
442 310,78125 8890 61,009765625 8897
443 311,484375 8886 65,009765625 8896
444 312,1875 8886 65,009765625 8894
445 312,890625 8879 72,009765625 8892
446 313,59375 8879 72,009765625 8888
447 314,296875 8878 73,009765625 8886
448 315 8873 78,009765625 8883
449 315,703125 8866 85,009765625 8878
450 316,40625 8866 85,009765625 8875
451 317,109375 8866 85,009765625 8873
452 317,8125 8860 91,009765625 8872
453 318,515625 8859 92,009765625 8868
454 319,21875 8857 94,009765625 8868
455 319,921875 8857 94,009765625 8864
456 320,625 8847 104,009765625 8860
457 321,328125 8847 104,009765625 8857
458 322,03125 8842 109,009765625 8852
459 322,734375 8841 110,009765625 8852
460 323,4375 8844 107,009765625 8853
461 324,140625 8846 105,009765625 8857
462 324,84375 8841 110,009765625 8853
463 325,546875 8836 115,009765625 8846
464 326,25 8839 112,009765625 8844
465 326,953125 8841 110,009765625 8846
466 327,65625 8839 112,009765625 8847
467 328,359375 8842 109,009765625 8852
468 329,0625 8836 115,009765625 8844
469 329,765625 8842 109,009765625 8847
470 330,46875 8842 109,009765625 8849
471 331,171875 8849 102,009765625 8860
472 331,875 8857 94,009765625 8864
473 332,578125 8852 99,009765625 8859
474 333,28125 8852 99,009765625 8862
475 333,984375 8855 96,009765625 8862
476 334,6875 8857 94,009765625 8866
477 335,390625 8862 89,009765625 8870
478 336,09375 8864 87,009765625 8875
479 336,796875 8868 83,009765625 8878
480 337,5 8872 79,009765625 8884
481 338,203125 8881 70,009765625 8888
482 338,90625 8886 65,009765625 8894
483 339,609375 8884 67,009765625 8899
484 340,3125 8886 65,009765625 8899
485 341,015625 8897 54,009765625 8909
486 341,71875 8901 50,009765625 8910
487 342,421875 8903 48,009765625 8916
488 343,125 8914 37,009765625 8923
489 343,828125 8925 26,009765625 8929
490 344,53125 8929 22,009765625 8934
491 345,234375 8927 24,009765625 8938
492 345,9375 8927 24,009765625 8936
493 346,640625 8930 21,009765625 8941
494 347,34375 8947 4,009765625 8954
495 348,046875 8947 4,009765625 8960
496 348,75 8958 -6,990234375 8967
497 349,453125 8960 -8,990234375 8973
498 350,15625 8965 -13,990234375 8977
499 350,859375 8967 -15,990234375 8978
500 351,5625 8964 -12,990234375 8971
501 352,265625 8962 -10,990234375 8973
502 352,96875 8971 -19,990234375 8983
503 353,671875 8977 -25,990234375 8985
504 354,375 8983 -31,990234375 8994
505 355,078125 8989 -37,990234375 8998
506 355,78125 8994 -42,990234375 9007
507 356,484375 9004 -52,990234375 9009
508 357,1875 9002 -50,990234375 9020
509 357,890625 9011 -59,990234375 9025
510 358,59375 9017 -65,990234375 9027
511 359,296875 9022 -70,990234375 9027
512 360 9022 -70,990234375 9038
答案 0 :(得分:10)
这种问题受到当地极端事件的困扰。 通过扩展正弦,您可以将模型重写为
C + alpha * sin(omega*T) + beta * cos(omega*T)
只剩下一个非线性参数 你可以使用网格搜索(或一些强大的优化算法) 在那个参数上 (和其他人的线性回归)。
d <- read.table( "tmp.csv", dec=",", header=TRUE, nrows=400)
x <- d[,1]
y <- d[,3]
f <- function( omega ) {
x1 <- sin( omega * x )
x2 <- cos( omega * x )
r <- lm( y ~ x1 + x2 )
res <- mean( residuals(r)^2 )
attr( res, "coef" ) <- coef(r)
res
}
omegas <- seq( .001, .2, length=1000 )
res <- sapply(omegas, f)
plot(
omegas, res,
las=1,
ylab = "Residuals", xlab = "Omega",
main = "Objective function: multiple local minima"
)
i <- which.min( res )
omega0 <- optimize(f, interval = c(omegas[i-1], omegas[i+1]))$minimum
p <- c( attr( f(omega0), "coef" ), omega0 )
plot( x, y )
lines(
x,
p[1] + p[2] * sin( p[4] * x ) + p[3] * cos( p[4] * x ),
col = "orange", lwd=3
)
答案 1 :(得分:6)
首先,我用“。”替换了数据中的所有“,”。 (或者您可以使用dec
的{{1}}参数),然后删除包含较少元素的行(最后一行)并创建一个合适的标题。
然后我使用read.table
读取您的数据。
然后我这样做了:
data <- read.table(text="<paste the cleaned data here>", header=TRUE)
得到了这个:
values<-data[,3]
T <-data[,1]
r<-nls(values~C+alpha*sin(W*T+phi),
start=list(C=8958.34, alpha=115.886, W=0.0652, phi=14.9286))
summary(r)
然后我画了:
Formula: values ~ C + alpha * sin(W * T + phi)
Parameters:
Estimate Std. Error t value Pr(>|t|)
C 8.959e+03 3.892e+00 2302.173 < 2e-16 ***
alpha 2.214e+01 5.470e+00 4.047 6.16e-05 ***
W 6.714e-02 2.031e-03 33.065 < 2e-16 ***
phi 1.334e+01 5.113e-01 26.092 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 80.02 on 423 degrees of freedom
Number of iterations to convergence: 21
Achieved convergence tolerance: 5.952e-06
得到了这个:
然后我读到了这个:https://stats.stackexchange.com/a/60997/11849
这样做了:
plot(values~T)
lines(predict(r)~T)
得到了这个:
而且:
raw.fft = fft(values)
truncated.fft = raw.fft[seq(1, length(values)/2 - 1)]
truncated.fft[1] = 0
W = which.max(abs(truncated.fft)) * 2 * pi / length(values)
r2<-nls(values~C+alpha*sin(W*T+phi), start=list(C=8958.34, alpha=115.886, W=W, phi=0))
lines(predict(r2)~T, col="red")
summary(r2)
PS:请注意,调用变量Formula: values ~ C + alpha * sin(W * T + phi)
Parameters:
Estimate Std. Error t value Pr(>|t|)
C 8.958e+03 2.045e-01 43804.2 <2e-16 ***
alpha 1.160e+02 2.913e-01 398.0 <2e-16 ***
W 4.584e-02 1.954e-05 2345.6 <2e-16 ***
phi 2.325e+00 4.760e-03 488.5 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 4.204 on 423 degrees of freedom
Number of iterations to convergence: 9
Achieved convergence tolerance: 1.07e-06
是一个非常糟糕的主意。 T
是R中T
的别名。