Adding geom_vline to ggplot2 from separate data frame - r

I am having trouble adding geom_vline to my ggplot 2 chart. I am plotting on the x axis the date. as.Date format. Then on the Y axis plotting numerical values.
On the same plot. I have matching dates pulled from another data frame. They are in the same Date format as my plot above.
The problem when adding the geom_vline is that it throws a error:
Error: ggplot2 doesn't know how to deal with data of class numeric
If my two dates I want to use are both Date format, im not sure why its throwing a numerical error date. The only numerial format I have is the data on the Y axis.
See code below:
p1 <- ggplot(data=plotdataframe, aes(x=ddate, y=SP500close1, colour=SP500close1)) +
geom_vline(data=matches.df,aes(xintercept=as.numeric(ddatesmatches)))+
geom_line() +
theme_bw() +
labs(title="S&P500 Close",y="S&P500 Close",x="Date")+
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5))
grid.arrange(p1)
Here is the formats of all the data that make this plot:
> str(matches.df)
'data.frame': 48 obs. of 1 variable:
$ ddatesmatches: Date, format: "1929-10-28" "1929-11-11" "1929-11-20" "1931-06-08" ...
> str(ddate)
Date[1:23539], format: "1928-01-03" "1928-01-04" "1928-01-05" "1928-01-06" "1928-01-07" "1928-01-09" "1928-01-10" "1928-01-11" ...
> str(SP500close1)
num [1:23539] 17.8 17.7 17.6 17.7 17.7 ...
As you can see.... both dates are in the Date format and my Y axis (SP500close1) is a numerical value.
Seems a simple thing to add to this chart. Since my formats are where they need to be, can anyone shed some insight into this?
Note - They are however from two different data sources, so i specified in geom_vline(data=matches.df.... in order to read the data to plot the vlines from that specific data frame.
Any insight appreciated.
Thanks
Here is the data for plotdataframe
ddate SP500close1
1 1928-01-03 17.76
2 1928-01-04 17.72
3 1928-01-05 17.55
4 1928-01-06 17.66
5 1928-01-07 17.68
6 1928-01-09 17.50
7 1928-01-10 17.37
8 1928-01-11 17.35
9 1928-01-12 17.47
10 1928-01-13 17.58
11 1928-01-14 17.44
12 1928-01-16 17.29
13 1928-01-17 17.30
14 1928-01-18 17.26
15 1928-01-19 17.38
16 1928-01-20 17.48
17 1928-01-21 17.53
18 1928-01-23 17.64
19 1928-01-24 17.71
20 1928-01-25 17.52
21 1928-01-26 17.63
22 1928-01-27 17.69
23 1928-01-28 17.58
24 1928-01-30 17.49
25 1928-01-31 17.57
26 1928-02-01 17.53
27 1928-02-02 17.63
28 1928-02-03 17.40
29 1928-02-04 17.43
30 1928-02-06 17.45
31 1928-02-07 17.44
32 1928-02-08 17.49
33 1928-02-09 17.55
34 1928-02-10 17.54
35 1928-02-11 17.53
36 1928-02-14 17.44
37 1928-02-15 17.40
38 1928-02-16 17.35
39 1928-02-17 17.04
40 1928-02-18 16.99
41 1928-02-20 16.95
42 1928-02-21 17.11
43 1928-02-23 17.13
44 1928-02-24 17.18
45 1928-02-25 17.19
46 1928-02-27 17.11
47 1928-02-28 17.16
48 1928-02-29 17.26
49 1928-03-01 17.30
50 1928-03-02 17.30
51 1928-03-03 17.42
52 1928-03-05 17.57
53 1928-03-06 17.67
54 1928-03-07 17.58
55 1928-03-08 17.64
56 1928-03-09 17.93
57 1928-03-10 17.87
58 1928-03-12 18.00
59 1928-03-13 17.92
60 1928-03-14 17.93
61 1928-03-15 18.07
62 1928-03-16 18.26
63 1928-03-17 18.32
64 1928-03-19 18.36
65 1928-03-20 18.46
66 1928-03-21 18.65
67 1928-03-22 18.60
68 1928-03-23 18.78
69 1928-03-24 18.79
70 1928-03-26 19.05
71 1928-03-27 18.94
72 1928-03-28 18.89
73 1928-03-29 18.95
74 1928-03-30 19.28
75 1928-03-31 19.13
76 1928-04-02 18.91
77 1928-04-03 19.02
78 1928-04-04 18.99
79 1928-04-05 19.23
80 1928-04-09 19.27
81 1928-04-10 19.10
82 1928-04-11 19.44
83 1928-04-12 19.41
84 1928-04-13 19.73
85 1928-04-14 19.59
86 1928-04-16 19.68
87 1928-04-17 19.52
88 1928-04-18 19.52
89 1928-04-19 19.63
90 1928-04-20 19.34
91 1928-04-23 19.15
92 1928-04-24 19.25
93 1928-04-25 19.33
94 1928-04-26 19.47
95 1928-04-27 19.68
96 1928-04-28 19.75
97 1928-04-30 19.75
98 1928-05-01 19.78
99 1928-05-02 19.83
100 1928-05-03 19.93
101 1928-05-04 20.24
102 1928-05-07 20.41
103 1928-05-08 20.27
104 1928-05-09 20.24
105 1928-05-10 20.21
106 1928-05-11 20.36
107 1928-05-14 20.44
108 1928-05-15 20.39
109 1928-05-16 20.05
110 1928-05-17 20.12
111 1928-05-18 19.92
112 1928-05-21 19.63
113 1928-05-22 19.36
114 1928-05-23 19.75
115 1928-05-24 19.87
116 1928-05-25 19.90
117 1928-05-28 19.57
118 1928-05-29 19.82
119 1928-05-31 20.00
120 1928-06-01 20.07
121 1928-06-02 20.22
122 1928-06-04 19.80
123 1928-06-05 19.85
124 1928-06-06 19.66
125 1928-06-07 19.61
126 1928-06-08 19.37
127 1928-06-09 19.12
128 1928-06-11 18.68
129 1928-06-12 18.34
130 1928-06-13 18.89
131 1928-06-14 19.05
132 1928-06-15 18.74
133 1928-06-16 18.75
134 1928-06-18 18.40
135 1928-06-19 18.34
136 1928-06-20 18.57
137 1928-06-21 18.56
138 1928-06-22 18.54
139 1928-06-23 18.46
140 1928-06-25 18.47
141 1928-06-26 18.68
142 1928-06-27 18.86
143 1928-06-28 19.07
144 1928-06-29 19.14
145 1928-06-30 19.19
146 1928-07-02 18.98
147 1928-07-03 19.29
148 1928-07-05 19.48
149 1928-07-06 19.39
150 1928-07-07 19.44
151 1928-07-09 19.46
152 1928-07-10 19.43
153 1928-07-11 18.95
154 1928-07-12 18.81
155 1928-07-13 18.96
156 1928-07-14 18.93
157 1928-07-16 18.70
158 1928-07-17 18.80
159 1928-07-18 19.03
160 1928-07-19 18.99
161 1928-07-20 19.03
162 1928-07-21 18.96
163 1928-07-23 19.12
164 1928-07-24 19.09
165 1928-07-25 19.20
166 1928-07-26 19.24
167 1928-07-27 19.42
168 1928-07-28 19.47
169 1928-07-30 19.47
170 1928-07-31 19.43
171 1928-08-01 19.42
172 1928-08-02 19.33
173 1928-08-03 19.36
174 1928-08-04 19.41
175 1928-08-06 19.58
176 1928-08-07 19.49
177 1928-08-08 19.25
178 1928-08-09 19.30
179 1928-08-10 19.25
180 1928-08-11 19.30
181 1928-08-13 19.29
182 1928-08-14 19.13
183 1928-08-15 19.47
184 1928-08-16 19.61
185 1928-08-17 19.64
186 1928-08-18 19.71
187 1928-08-20 19.77
188 1928-08-21 20.02
189 1928-08-22 20.00
190 1928-08-23 20.02
191 1928-08-24 20.34
192 1928-08-25 20.45
193 1928-08-27 20.38
194 1928-08-28 20.54
195 1928-08-29 20.58
196 1928-08-30 20.61
197 1928-08-31 20.87
198 1928-09-01 20.87
199 1928-09-04 20.97
200 1928-09-05 21.07
201 1928-09-06 20.94
202 1928-09-07 21.09
203 1928-09-08 21.03
204 1928-09-10 20.92
205 1928-09-11 21.08
206 1928-09-12 21.16
207 1928-09-13 21.14
208 1928-09-14 21.14
209 1928-09-15 21.23
210 1928-09-17 21.33
211 1928-09-18 21.18
212 1928-09-19 21.18
213 1928-09-20 21.22
214 1928-09-21 21.36
215 1928-09-22 21.24
216 1928-09-24 21.43
217 1928-09-25 21.41
218 1928-09-26 21.40
219 1928-09-27 21.13
220 1928-09-28 21.14
221 1928-09-29 21.37
222 1928-10-01 21.36
223 1928-10-02 21.26
224 1928-10-03 21.19
225 1928-10-04 21.26
226 1928-10-05 21.22
227 1928-10-06 21.17
228 1928-10-08 21.11
229 1928-10-09 21.18
230 1928-10-10 21.41
231 1928-10-11 21.51
232 1928-10-13 21.63
233 1928-10-15 21.65
234 1928-10-16 21.72
235 1928-10-17 21.79
236 1928-10-18 21.84
237 1928-10-19 21.96
238 1928-10-20 21.84
239 1928-10-22 21.81
240 1928-10-23 21.95
241 1928-10-24 21.99
242 1928-10-25 21.91
243 1928-10-26 21.65
244 1928-10-27 21.87
245 1928-10-29 22.00
246 1928-10-30 21.76
247 1928-10-31 21.68
248 1928-11-01 21.99
249 1928-11-02 21.95
250 1928-11-03 21.97
251 1928-11-05 22.24
252 1928-11-07 22.50
253 1928-11-08 22.39
254 1928-11-09 22.55
255 1928-11-10 22.73
256 1928-11-12 22.84
257 1928-11-13 22.91
258 1928-11-14 22.91
259 1928-11-15 23.03
260 1928-11-16 23.37
261 1928-11-17 23.37
262 1928-11-19 23.37
263 1928-11-20 23.57
264 1928-11-21 23.22
265 1928-11-22 23.52
266 1928-11-23 23.70
267 1928-11-26 23.80
268 1928-11-27 23.91
269 1928-11-28 24.19
270 1928-11-30 24.28
271 1928-12-01 24.03
272 1928-12-03 23.85
273 1928-12-04 23.91
274 1928-12-05 23.81
275 1928-12-06 22.91
276 1928-12-07 22.36
277 1928-12-08 21.92
278 1928-12-10 22.31
279 1928-12-11 22.74
280 1928-12-12 22.52
281 1928-12-13 22.48
282 1928-12-14 22.53
283 1928-12-15 22.52
284 1928-12-17 22.44
285 1928-12-18 22.70
286 1928-12-19 22.98
287 1928-12-20 23.07
288 1928-12-21 23.33
289 1928-12-22 23.33
290 1928-12-24 23.54
291 1928-12-26 23.44
292 1928-12-27 23.64
293 1928-12-28 23.98
294 1928-12-29 24.05
295 1928-12-31 24.35
296 1929-01-02 24.81
297 1929-01-03 24.86
298 1929-01-04 24.85
299 1929-01-05 24.57
300 1929-01-07 24.25
301 1929-01-08 24.17
302 1929-01-09 24.54
303 1929-01-10 24.58
304 1929-01-11 24.54
305 1929-01-12 24.47
306 1929-01-14 24.59
307 1929-01-15 24.23
308 1929-01-16 24.56
309 1929-01-17 24.69
310 1929-01-18 24.77
311 1929-01-19 24.88
312 1929-01-21 24.86
313 1929-01-22 25.07
314 1929-01-23 25.22
315 1929-01-24 25.06
316 1929-01-25 25.48
317 1929-01-26 25.43
318 1929-01-28 25.35
319 1929-01-29 25.36
320 1929-01-30 25.45
321 1929-01-31 25.74
322 1929-02-01 25.84
323 1929-02-02 25.80
324 1929-02-04 25.64
325 1929-02-05 25.70
326 1929-02-06 25.49
327 1929-02-07 24.71
328 1929-02-08 24.44
329 1929-02-11 24.98
330 1929-02-13 24.95
331 1929-02-14 24.85
332 1929-02-15 24.35
333 1929-02-16 24.05
334 1929-02-18 24.39
335 1929-02-19 24.38
336 1929-02-20 24.59
337 1929-02-21 24.86
338 1929-02-25 24.91
339 1929-02-26 24.95
340 1929-02-27 25.25
341 1929-02-28 25.59
342 1929-03-01 25.83
343 1929-03-02 25.77
344 1929-03-04 25.49
345 1929-03-05 25.32
346 1929-03-06 24.92
347 1929-03-07 25.15
348 1929-03-08 25.31
349 1929-03-09 25.31
350 1929-03-11 24.95
351 1929-03-12 24.97
352 1929-03-13 25.24
353 1929-03-14 25.73
354 1929-03-15 26.08
355 1929-03-16 26.13
356 1929-03-18 26.04
357 1929-03-19 26.03
358 1929-03-20 26.02
359 1929-03-21 25.90
360 1929-03-22 25.55
361 1929-03-23 25.21
362 1929-03-25 24.51
363 1929-03-26 24.35
364 1929-03-27 25.09
365 1929-03-28 25.53
366 1929-04-01 24.88
367 1929-04-02 25.17
368 1929-04-03 24.86
369 1929-04-04 25.30
370 1929-04-05 25.05
371 1929-04-06 25.07
372 1929-04-08 24.83
373 1929-04-09 24.63
374 1929-04-10 24.78
375 1929-04-11 25.06
376 1929-04-12 25.15
377 1929-04-13 25.17
378 1929-04-15 25.00
379 1929-04-16 25.06
380 1929-04-17 25.32
381 1929-04-18 25.43
382 1929-04-19 25.41
383 1929-04-20 25.45
384 1929-04-22 25.70
385 1929-04-23 25.85
386 1929-04-24 25.75
387 1929-04-25 25.63
388 1929-04-26 25.58
389 1929-04-27 25.65
390 1929-04-29 25.53
391 1929-04-30 25.94
392 1929-05-01 25.99
393 1929-05-02 26.11
394 1929-05-03 26.37
395 1929-05-04 26.48
396 1929-05-06 26.32
397 1929-05-07 26.07
398 1929-05-08 26.18
399 1929-05-09 26.02
400 1929-05-10 26.34
401 1929-05-11 26.24
402 1929-05-13 25.64
403 1929-05-14 25.95
404 1929-05-15 25.84
405 1929-05-16 25.97
406 1929-05-17 26.09
407 1929-05-18 26.05
408 1929-05-20 25.39
409 1929-05-21 25.70
410 1929-05-22 24.76
411 1929-05-23 25.31
412 1929-05-24 25.12
413 1929-05-25 25.03
414 1929-05-27 24.20
415 1929-05-28 24.59
416 1929-05-29 24.62
417 1929-05-31 24.83
418 1929-06-01 24.92
419 1929-06-03 25.27
420 1929-06-04 25.64
421 1929-06-05 25.59
422 1929-06-06 25.64
423 1929-06-07 25.53
424 1929-06-08 25.39
425 1929-06-10 25.27
426 1929-06-11 25.43
427 1929-06-12 25.45
428 1929-06-13 25.84
429 1929-06-14 25.93
430 1929-06-15 26.05
431 1929-06-17 26.41
432 1929-06-18 26.42
433 1929-06-19 26.32
434 1929-06-20 26.47
435 1929-06-21 26.62
436 1929-06-22 26.72
437 1929-06-24 26.62
438 1929-06-25 26.90
439 1929-06-26 27.06
440 1929-06-27 27.11
441 1929-06-28 27.41
442 1929-06-29 27.62
443 1929-07-01 27.77
444 1929-07-02 28.06
445 1929-07-03 28.18
446 1929-07-05 28.32
447 1929-07-06 28.36
448 1929-07-08 28.37
449 1929-07-09 28.28
450 1929-07-10 28.20
451 1929-07-11 28.26
452 1929-07-12 28.57
453 1929-07-13 28.64
454 1929-07-15 28.45
455 1929-07-16 28.65
456 1929-07-17 28.72
457 1929-07-18 28.66
458 1929-07-19 28.76
459 1929-07-20 28.81
460 1929-07-22 28.43
461 1929-07-23 28.83
462 1929-07-24 28.64
463 1929-07-25 28.77
464 1929-07-26 28.66
465 1929-07-27 28.55
466 1929-07-29 28.18
467 1929-07-30 28.50
468 1929-07-31 28.88
469 1929-08-01 29.08
470 1929-08-02 29.56
471 1929-08-03 29.81
472 1929-08-05 29.54
473 1929-08-06 29.43
474 1929-08-07 29.13
475 1929-08-08 29.47
476 1929-08-09 28.38
477 1929-08-10 28.83
478 1929-08-12 29.27
479 1929-08-13 29.53
480 1929-08-14 29.62
481 1929-08-15 29.67
482 1929-08-16 30.22
483 1929-08-17 30.24
484 1929-08-19 30.50
485 1929-08-20 30.68
486 1929-08-21 30.55
487 1929-08-22 30.79
488 1929-08-23 31.09
489 1929-08-24 31.15
490 1929-08-26 31.07
491 1929-08-27 31.05
492 1929-08-28 30.99
493 1929-08-29 31.29
494 1929-08-30 31.71
495 1929-09-03 31.82
496 1929-09-04 31.72
497 1929-09-05 31.02
498 1929-09-06 31.83
499 1929-09-07 31.92
500 1929-09-09 31.67
And for the matches.df
ddatesmatches
1 1929-10-28
2 1929-11-11
3 1929-11-20

This issue was solved by adding: scale_x_date()+ to the code
p1 <- ggplot(data=plotdataframe, aes(x=ddate, y=SP500close1, colour=SP500close1)) +
scale_x_date()+
geom_vline(data=matches.df,aes(xintercept=as.numeric(ddatesmatches)))+
geom_line() +
theme_bw() +
labs(title="S&P500 Close",y="S&P500 Close",x="Date")+
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5))
grid.arrange(p1)

Related

Choosing rows from a matrix in a specific order

I have a matrix of order 2192*23, I want to choose the rows number "3rd", "4th", "10th", "11th", "17th", "18th", "24th", "25th" and so on
[3,4,10,11,17,18,24,25,31,32,38,39,45,46,...]
continue till the last row in the above order. how I will do this in R?
a1 <- c(3, 4)
d <- 7
n <- 10
S1 <- a1[1] + d * (seq(n) - 1)
S2 <- a1[2] + d * (seq(n) - 1)
sort(c(S1, S2))
#> [1] 3 4 10 11 17 18 24 25 31 32 38 39 45 46 52 53 59 60 66 67
library(magrittr)
sapply(a1, function(x) (x + d * (seq(n) - 1))) %>%
matrix(ncol = 1) %>%
sort
#> [1] 3 4 10 11 17 18 24 25 31 32 38 39 45 46 52 53 59 60 66 67
Created on 2022-01-18 by the reprex package (v2.0.1)
We can use outer + seq
> c(outer(0:1, seq(3, 2192, by = 7), `+`))
[1] 3 4 10 11 17 18 24 25 31 32 38 39 45 46 52
[16] 53 59 60 66 67 73 74 80 81 87 88 94 95 101 102
[31] 108 109 115 116 122 123 129 130 136 137 143 144 150 151 157
[46] 158 164 165 171 172 178 179 185 186 192 193 199 200 206 207
[61] 213 214 220 221 227 228 234 235 241 242 248 249 255 256 262
[76] 263 269 270 276 277 283 284 290 291 297 298 304 305 311 312
[91] 318 319 325 326 332 333 339 340 346 347 353 354 360 361 367
[106] 368 374 375 381 382 388 389 395 396 402 403 409 410 416 417
[121] 423 424 430 431 437 438 444 445 451 452 458 459 465 466 472
[136] 473 479 480 486 487 493 494 500 501 507 508 514 515 521 522
[151] 528 529 535 536 542 543 549 550 556 557 563 564 570 571 577
[166] 578 584 585 591 592 598 599 605 606 612 613 619 620 626 627
[181] 633 634 640 641 647 648 654 655 661 662 668 669 675 676 682
[196] 683 689 690 696 697 703 704 710 711 717 718 724 725 731 732
[211] 738 739 745 746 752 753 759 760 766 767 773 774 780 781 787
[226] 788 794 795 801 802 808 809 815 816 822 823 829 830 836 837
[241] 843 844 850 851 857 858 864 865 871 872 878 879 885 886 892
[256] 893 899 900 906 907 913 914 920 921 927 928 934 935 941 942
[271] 948 949 955 956 962 963 969 970 976 977 983 984 990 991 997
[286] 998 1004 1005 1011 1012 1018 1019 1025 1026 1032 1033 1039 1040 1046 1047
[301] 1053 1054 1060 1061 1067 1068 1074 1075 1081 1082 1088 1089 1095 1096 1102
[316] 1103 1109 1110 1116 1117 1123 1124 1130 1131 1137 1138 1144 1145 1151 1152
[331] 1158 1159 1165 1166 1172 1173 1179 1180 1186 1187 1193 1194 1200 1201 1207
[346] 1208 1214 1215 1221 1222 1228 1229 1235 1236 1242 1243 1249 1250 1256 1257
[361] 1263 1264 1270 1271 1277 1278 1284 1285 1291 1292 1298 1299 1305 1306 1312
[376] 1313 1319 1320 1326 1327 1333 1334 1340 1341 1347 1348 1354 1355 1361 1362
[391] 1368 1369 1375 1376 1382 1383 1389 1390 1396 1397 1403 1404 1410 1411 1417
[406] 1418 1424 1425 1431 1432 1438 1439 1445 1446 1452 1453 1459 1460 1466 1467
[421] 1473 1474 1480 1481 1487 1488 1494 1495 1501 1502 1508 1509 1515 1516 1522
[436] 1523 1529 1530 1536 1537 1543 1544 1550 1551 1557 1558 1564 1565 1571 1572
[451] 1578 1579 1585 1586 1592 1593 1599 1600 1606 1607 1613 1614 1620 1621 1627
[466] 1628 1634 1635 1641 1642 1648 1649 1655 1656 1662 1663 1669 1670 1676 1677
[481] 1683 1684 1690 1691 1697 1698 1704 1705 1711 1712 1718 1719 1725 1726 1732
[496] 1733 1739 1740 1746 1747 1753 1754 1760 1761 1767 1768 1774 1775 1781 1782
[511] 1788 1789 1795 1796 1802 1803 1809 1810 1816 1817 1823 1824 1830 1831 1837
[526] 1838 1844 1845 1851 1852 1858 1859 1865 1866 1872 1873 1879 1880 1886 1887
[541] 1893 1894 1900 1901 1907 1908 1914 1915 1921 1922 1928 1929 1935 1936 1942
[556] 1943 1949 1950 1956 1957 1963 1964 1970 1971 1977 1978 1984 1985 1991 1992
[571] 1998 1999 2005 2006 2012 2013 2019 2020 2026 2027 2033 2034 2040 2041 2047
[586] 2048 2054 2055 2061 2062 2068 2069 2075 2076 2082 2083 2089 2090 2096 2097
[601] 2103 2104 2110 2111 2117 2118 2124 2125 2131 2132 2138 2139 2145 2146 2152
[616] 2153 2159 2160 2166 2167 2173 2174 2180 2181 2187 2188
Using vector recycling -
ind <- seq_len(nrow(mat))
#ind <- seq_len(2192)
ind[c(F, F, T, T, F, F, F)]
#[1] 3 4 10 11 17 18 24 25 31 32 38 39 45 46 52
#[16] 53 59 60 66 67 73 74 80 81 87 88 94 95 101 102
#...
#...
Another way is to use sequence
n = 2192/7
s <- sequence(nvec = rep(2, n), from = (7*1:n)-4)
# [1] 3 4 10 11 17 18 24 25 31 32 38 39 45 46 52 53 59 60 66 67 73 74
# ...

Combine all elements of a list and retain only unique values

I have a data frame SCC with several columns. From these columns I wanted to find all the rows that have the word "coal" in them.
I use the grep function to do so and stored results in variable x (a list) with the following command:
x <- sapply(SCC, grep, pattern="coal", ignore.case=T)
Now x tells me where in the data frame the word "coal" occurs, it is a list and the 4th and the 9th elements of this list are given below.
> x[4]
$EI.Sector
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[17] 17 18 19 20 21 22 23 24 25 26 27 28 29 30 80 81
[33] 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
[49] 99 100 101 102 103 104 105 106 107 108 109 110 111 112 161 162
[65] 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
[81] 179 180 181 182 183 184 185 215 221 433 434 435 447 448 462 463
[97] 527 528 529
> x[9]
$SCC.Level.Three
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13
[14] 14 15 16 17 18 19 20 21 79 80 81 83 84
[27] 85 86 87 88 89 90 91 92 93 94 95 96 97
[40] 98 99 100 101 102 103 104 105 161 162 163 164 165
[53] 166 167 168 169 170 171 172 173 174 175 176 177 178
[66] 179 180 242 433 434 435 447 448 462 463 477 478 527
[79] 528 529 2220 2221 2222 2223 2224 2496 2497 2498 2499 2500 2501
[92] 2502 2503 2504 2505 2506 2591 2592 4520 4673 4674 4675 4676 4677
[105] 4678 4679 4680 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424
[118] 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437
[131] 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450
[144] 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7991
[157] 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004
[170] 8005 8006 8007 8008 8009 9054 9055 9056 9057 9058 9059 9060
Question 1
I want to combine x[4] and x[9] into one index; how should I proceed? I am right now using below code, but is there a more concise command?
y <- x[[4]] %in% intersect(x[[4]], x[[9]])
z <- c( x[[4]][!y], x[[9]])
> sort(z)
[1] 1 2 3 4 5 6 7 8 9 10 11 12
[13] 13 14 15 16 17 18 19 20 21 22 23 24
[25] 25 26 27 28 29 30 79 80 81 83 84 85
[37] 86 87 88 89 90 91 92 93 94 95 96 97
[49] 98 99 100 101 102 103 104 105 106 107 108 109
[61] 110 111 112 161 162 163 164 165 166 167 168 169
[73] 170 171 172 173 174 175 176 177 178 179 180 181
[85] 182 183 184 185 215 221 242 433 434 435 447 448
[97] 462 463 477 478 527 528 529 2220 2221 2222 2223 2224
[109] 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2591
[121] 2592 4520 4673 4674 4675 4676 4677 4678 4679 4680 7415 7416
[133] 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428
[145] 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440
[157] 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452
[169] 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7991 7992
[181] 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004
[193] 8005 8006 8007 8008 8009 9054 9055 9056 9057 9058 9059 9060
Question 2
There are other columns with the word "coal" in SCC. Length of the elements of x gives us and idea of where the word "coal" is used in SCC. Is there a single command to combine all the elements of x to generate row index?
sapply(x, NROW)
SCC Data.Category Short.Name
0 0 239
EI.Sector Option.Group Option.Set
99 0 0
SCC.Level.One SCC.Level.Two SCC.Level.Three
0 0 181
SCC.Level.Four Map.To Last.Inventory.Year
126 0 0
Created_Date Revised_Date Usage.Notes
0 0 0
(making comments into answer)
Are you not doing z <- union(x[[4]], x[[9]])?
union does the job, is there a way to do union on all the element of x simultaneously?
union() takes only two vectors; is there another function that would take as argument all the elements of x?
We can simply do
unique(unlist(x))
If you want to stay with using union, do:
Reduce(union, x)

Remove a white block space

I'm trying to merge this two figures in a way that the rmsf image sits on top of the schematics.
I used the code below to generate the correct figure, but I can't find a way to remove the massive white area at the bottom of the plot (the huge gap that you see between the figure and the code block).
I could crop it with gimp or other software, but I'd prefer to code it correctly.
args <- (commandArgs(T))
library(gdata)
library(ggplot2)
library(plotflow)
library(dplyr)
library(grid)
rmsf.schem <- read.table("./schematics_bindingSites",header=T,sep="",quote="")
rmsf.data <- read.table(paste(args[1]),header=F,sep="",quote="")
names(rmsf.data) <- c("residue","nm")
bb.ggplot <- ggplot(rmsf.data,aes(x=rmsf.data$residue),environment = environment())
gg.rmsf <- bb.ggplot+ geom_line(aes(y=rmsf.data$nm)) +
labs(y = "nm", x = NULL, title = NULL) +
theme(legend.position="none",plot.margin=unit(c(1,1,0,1), "line"))
binding.sites <- ggplot(rmsf.schem, running=order_by(Domain, ~Start), aes(x = Type, ymin = Start, ymax = Stop, colour = Domain)) +
scale_color_manual(values=c("white", "red","blue")) +
geom_linerange(size = 3) + coord_flip() + theme_bw() +
labs(y = NULL, x = NULL, title = NULL) + theme(axis.line = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), panel.border = element_blank(),
panel.background = element_blank(), plot.background= element_blank(), axis.ticks = element_blank(),
legend.position ="none", axis.text = element_blank(), plot.margin=unit(c(-15,1,0,1), "line"))
gA <- ggplotGrob(gg.rmsf)
gB <- ggplotGrob(binding.sites)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
hello <- grid.arrange(gA, gB, ncol=1)
ggsave("ciao.png",hello, height=7,width=11,dpi=300)
I found several similar questions on stackoverflow, but none that was able to solve this behaviour.
The problem is in the binding.sites object; being a single value I couldn't act on ylim to reduce the area, so I make the background transparent and use a negative margin plot.margin=unit(c(-15,1,0,1) to get the schematics close to the upper plot. I tried to further change plot.margins and change height and width within ggsave, but the white area is always there.
the file used to generate this figure are:
schematics_bindingSites
Domain Type Start Stop
chain bindingSite 0 505
g6p bindingSite 198 206
coEnz bindingSite 38 44
rmsf file
27 0.5757
28 0.4123
29 0.2642
30 0.1848
31 0.1562
32 0.1362
33 0.1258
34 0.1193
35 0.1183
36 0.1207
37 0.1250
38 0.1374
39 0.1459
40 0.1684
41 0.1621
42 0.1766
43 0.1480
44 0.1266
45 0.1566
46 0.1578
47 0.1288
48 0.1132
49 0.1263
50 0.1295
51 0.1112
52 0.1131
53 0.1295
54 0.1240
55 0.1163
56 0.1348
57 0.1487
58 0.1457
59 0.1452
60 0.1245
61 0.1255
62 0.1469
63 0.1644
64 0.1543
65 0.1435
66 0.1426
67 0.1404
68 0.1432
69 0.1483
70 0.1501
71 0.1634
72 0.1825
73 0.1998
74 0.2202
75 0.2133
76 0.2144
77 0.2081
78 0.2197
79 0.2117
80 0.1904
81 0.1967
82 0.2127
83 0.2034
84 0.1857
85 0.2006
86 0.2105
87 0.1816
88 0.1752
89 0.1978
90 0.2062
91 0.2303
92 0.2545
93 0.2425
94 0.2201
95 0.2312
96 0.2364
97 0.2040
98 0.1935
99 0.2060
100 0.2004
101 0.1793
102 0.1858
103 0.1979
104 0.1786
105 0.1702
106 0.1705
107 0.1761
108 0.1775
109 0.1852
110 0.1794
111 0.1706
112 0.1533
113 0.1684
114 0.1748
115 0.1800
116 0.1940
117 0.1825
118 0.1684
119 0.1856
120 0.1904
121 0.1696
122 0.1731
123 0.1936
124 0.1854
125 0.1728
126 0.1926
127 0.2041
128 0.1918
129 0.1952
130 0.1895
131 0.1770
132 0.1864
133 0.1757
134 0.1507
135 0.1304
136 0.1157
137 0.1013
138 0.1025
139 0.0989
140 0.0960
141 0.1128
142 0.1341
143 0.1489
144 0.1483
145 0.1469
146 0.1319
147 0.1210
148 0.1220
149 0.1284
150 0.1191
151 0.1115
152 0.1244
153 0.1380
154 0.1296
155 0.1334
156 0.1531
157 0.1614
158 0.1462
159 0.1391
160 0.1416
161 0.1638
162 0.1651
163 0.1475
164 0.1312
165 0.1105
166 0.0945
167 0.0853
168 0.0769
169 0.0751
170 0.0944
171 0.1149
172 0.1292
173 0.1190
174 0.1285
175 0.1306
176 0.1204
177 0.1250
178 0.1235
179 0.1118
180 0.1000
181 0.1070
182 0.1089
183 0.1027
184 0.1040
185 0.1120
186 0.1089
187 0.1063
188 0.1180
189 0.1280
190 0.1170
191 0.1069
192 0.1007
193 0.0930
194 0.0998
195 0.0862
196 0.0697
197 0.0624
198 0.0701
199 0.0761
200 0.0785
201 0.0801
202 0.0826
203 0.0928
204 0.0815
205 0.0734
206 0.0755
207 0.0792
208 0.0737
209 0.0726
210 0.0797
211 0.0818
212 0.0836
213 0.0818
214 0.0787
215 0.0763
216 0.0795
217 0.0801
218 0.0821
219 0.0891
220 0.0955
221 0.0981
222 0.1000
223 0.0972
224 0.0876
225 0.0793
226 0.0782
227 0.0823
228 0.0908
229 0.0811
230 0.0780
231 0.0774
232 0.0720
233 0.0667
234 0.0673
235 0.0735
236 0.0803
237 0.0826
238 0.0892
239 0.0987
240 0.1103
241 0.1179
242 0.1221
243 0.1300
244 0.1600
245 0.1798
246 0.1589
247 0.1363
248 0.1369
249 0.1321
250 0.1231
251 0.1197
252 0.1221
253 0.1094
254 0.1021
255 0.0951
256 0.0894
257 0.0960
258 0.1065
259 0.0998
260 0.0900
261 0.0926
262 0.1163
263 0.0932
264 0.0762
265 0.0746
266 0.0732
267 0.0747
268 0.0731
269 0.0729
270 0.0727
271 0.0726
272 0.0766
273 0.0777
274 0.0874
275 0.0986
276 0.1040
277 0.1359
278 0.1519
279 0.2056
280 0.1788
281 0.1478
282 0.1409
283 0.1236
284 0.0995
285 0.0888
286 0.0974
287 0.0847
288 0.0815
289 0.0865
290 0.0924
291 0.0888
292 0.0864
293 0.0887
294 0.0866
295 0.0870
296 0.0940
297 0.0953
298 0.0965
299 0.1181
300 0.1239
301 0.1187
302 0.1006
303 0.0944
304 0.0952
305 0.0991
306 0.1035
307 0.1097
308 0.1153
309 0.1274
310 0.1422
311 0.1570
312 0.1954
313 0.1978
314 0.1853
315 0.1935
316 0.1917
317 0.1690
318 0.1522
319 0.1554
320 0.1486
321 0.1348
322 0.1272
323 0.1396
324 0.1479
325 0.1499
326 0.1654
327 0.1547
328 0.1452
329 0.1531
330 0.1722
331 0.1581
332 0.1331
333 0.1210
334 0.1120
335 0.1041
336 0.0999
337 0.0956
338 0.0885
339 0.0853
340 0.0809
341 0.0793
342 0.0784
343 0.0821
344 0.0872
345 0.0892
346 0.0881
347 0.0914
348 0.0845
349 0.0802
350 0.0903
351 0.0942
352 0.0809
353 0.0777
354 0.0735
355 0.0751
356 0.0770
357 0.0792
358 0.0825
359 0.0906
360 0.0990
361 0.1063
362 0.1073
363 0.1136
364 0.1094
365 0.1035
366 0.0867
367 0.0730
368 0.0682
369 0.0673
370 0.0670
371 0.0697
372 0.0727
373 0.0772
374 0.0876
375 0.1109
376 0.1202
377 0.1612
378 0.1777
379 0.2111
380 0.1768
381 0.1701
382 0.2152
383 0.1783
384 0.1313
385 0.1055
386 0.0953
387 0.0810
388 0.0734
389 0.0732
390 0.0736
391 0.0732
392 0.0719
393 0.0774
394 0.0795
395 0.0882
396 0.1033
397 0.1215
398 0.1025
399 0.0810
400 0.0788
401 0.0809
402 0.0845
403 0.0815
404 0.0811
405 0.0818
406 0.0830
407 0.0842
408 0.0964
409 0.1156
410 0.1213
411 0.1307
412 0.1222
413 0.1154
414 0.1069
415 0.0926
416 0.0875
417 0.0897
418 0.0833
419 0.0882
420 0.0889
421 0.0861
422 0.0916
423 0.0952
424 0.1088
425 0.1827
426 0.1998
427 0.1620
428 0.1472
429 0.2168
430 0.2671
431 0.3304
432 0.2871
433 0.2700
434 0.1976
435 0.1573
436 0.1736
437 0.0977
438 0.0827
439 0.0736
440 0.0662
441 0.0703
442 0.0767
443 0.0736
444 0.0754
445 0.0888
446 0.0934
447 0.0888
448 0.0820
449 0.0809
450 0.0947
451 0.0980
452 0.0726
453 0.0703
454 0.0735
455 0.0849
456 0.0945
457 0.0880
458 0.0867
459 0.0918
460 0.0829
461 0.0818
462 0.0856
463 0.0856
464 0.0822
465 0.0866
466 0.1037
467 0.1117
468 0.1061
469 0.1064
470 0.1209
471 0.1349
472 0.1383
473 0.1527
474 0.1589
475 0.1506
476 0.1422
477 0.1388
478 0.1338
479 0.1192
480 0.1221
481 0.1228
482 0.1178
483 0.1238
484 0.1236
485 0.1211
486 0.1188
487 0.1139
488 0.1152
489 0.1170
490 0.1243
491 0.1169
492 0.1038
493 0.1049
494 0.1089
495 0.0985
496 0.0959
497 0.1079
498 0.1084
499 0.1046
500 0.1180
501 0.1084
502 0.1051
503 0.1248
504 0.1750
505 0.2072
SOLUTION:
As drmariod suggested, I replaced grid.arrange with arrangeGrob. In this way I was able to set heights = c(0.9,0.1). I then change the margins in the schematics figure plot.margin=unit(c(-1.5,1,0,1), "line") and everything worked as expected.
Instead of going grobs route, how about this:
library(ggplot2)
#dummy data
set.seed(123)
rmsf <- data.frame(res=1:100,nm=runif(100))
bindingSite <- data.frame(
Domain=c("chain","g6p","coEnz"),
Type=rep("bindingSite",3),
Start=c(10,20,40),
Stop=c(15,22,48)
)
#plot
ggplot() +
geom_rect(aes(xmin=Start,xmax=Stop,ymin=-Inf,ymax=0,fill=Domain),
bindingSite,alpha=0.4) +
geom_line(aes(res,nm),rmsf) +
theme_classic()
As drmariod suggested, I replaced grid.arrange with arrangeGrob. In this way I was able to set heights = c(0.9,0.1). I then change the margins in the schematics figure plot.margin=unit(c(-1.5,1,0,1), "line") and everything worked as expected.
Also zx8754 solution works, but in the optics or adding more schematics to the same graph, I think that using arrangeGrob is the best option.

Speed up reading data in console using read.table(text="....") RStudio

I find myself pulling bits of data here and there from old excel files I have stored from past projects. These files are unorganized, I mean no common format of rows and columns so it would be useless to read the entire spreadsheet directly into R, and most of the time I just want to grab a couple columns and rows of data at a time anyway. Rather than creating a separate text file with the columns/rows I want and reading this file in I find the easiest way to get the data is to use read.table(text="....") and copy and paste the bit of data I want into text. This works great but when I start to grab larger data sets it takes longer for the console to process the information. I have the same issue sometimes when entering very large functions. I think this may be an RStudio limitation on how fast it shows the information passing in the console but I am not sure. I have to wait for the console to pass this information before I can do anything else. How can I get my console to either show me the information I'm reading faster or to not show me anything at all? Preferably the first.
Example Data
GNL <- read.table(header=TRUE,text="WYD Temp_C
1 12.77777778
2 11.66666667
3 8.888888889
4 3.888888889
5 -0.555555556
6 -1.111111111
7 3.888888889
8 7.777777778
9 8.333333333
10 6.666666667
11 10.55555556
12 15.55555556
13 16.11111111
14 16.66666667
15 15
16 13.33333333
17 14.44444444
18 13.88888889
19 11.66666667
20 12.77777778
21 12.22222222
22 15
23 14.44444444
24 11.11111111
25 7.222222222
26 5.555555556
27 6.666666667
28 8.888888889
29 11.66666667
30 11.66666667
31 10.55555556
32 7.777777778
33 8.333333333
34 2.777777778
35 -4.444444444
36 -5
37 -4.444444444
38 -1.666666667
39 0.555555556
40 5.555555556
41 NA
42 2.777777778
43 1.666666667
44 3.333333333
45 3.888888889
46 5
47 5.555555556
48 4.444444444
49 -1.111111111
50 -3.888888889
51 -3.888888889
52 -0.555555556
53 3.888888889
54 5.555555556
55 1.111111111
56 4.444444444
57 10
58 10
59 8.888888889
60 10
61 2.777777778
62 -3.333333333
63 1.666666667
64 -1.111111111
65 NA
66 NA
67 0
68 3.888888889
69 5
70 5.555555556
71 2.777777778
72 -0.555555556
73 -3.888888889
74 -3.333333333
75 -2.222222222
76 -1.666666667
77 3.888888889
78 6.111111111
79 1.666666667
80 2.222222222
81 5
82 3.333333333
83 0
84 4.444444444
85 5
86 5
87 5.555555556
88 6.111111111
89 8.888888889
90 7.222222222
91 5.555555556
92 7.777777778
93 10
94 8.888888889
95 9.444444444
96 11.11111111
97 8.888888889
98 6.111111111
99 5
100 7.777777778
101 7.777777778
102 5.555555556
103 6.111111111
104 5
105 6.111111111
106 5.555555556
107 0.555555556
108 -6.111111111
109 -2.222222222
110 2.777777778
111 1.666666667
112 2.222222222
113 -3.888888889
114 -3.333333333
115 NA
116 2.777777778
117 7.777777778
118 3.888888889
119 3.888888889
120 7.777777778
121 6.111111111
122 3.888888889
123 3.333333333
124 0.555555556
125 5.555555556
126 1.111111111
127 0.555555556
128 1.111111111
129 3.333333333
130 0
131 4.444444444
132 6.666666667
133 5
134 0
135 -1.111111111
136 -5.555555556
137 -2.777777778
138 -5
139 1.111111111
140 3.888888889
141 0
142 -2.222222222
143 0
144 6.666666667
145 8.333333333
146 8.888888889
147 7.777777778
148 2.777777778
149 -0.555555556
150 -5.555555556
151 -5.555555556
152 -5.555555556
153 -5
154 2.222222222
155 8.333333333
156 8.333333333
157 7.222222222
158 -5
159 -0.555555556
160 7.222222222
161 7.222222222
162 5
163 1.111111111
164 -0.555555556
165 -1.111111111
166 0
167 3.333333333
168 1.111111111
169 NA
170 -7.777777778
171 -6.666666667
172 NA
173 NA
174 NA
175 NA
176 2.777777778
177 -2.777777778
178 -2.777777778
179 NA
180 0.555555556
181 3.333333333
182 7.222222222
183 -0.555555556
184 -2.222222222
185 3.888888889
186 6.666666667
187 -1.111111111
188 -5.555555556
189 -1.666666667
190 6.111111111
191 7.777777778
192 7.777777778
193 3.888888889
194 -2.777777778
195 -3.333333333
196 -4.444444444
197 -2.777777778
198 3.333333333
199 6.111111111
200 6.666666667
201 7.222222222
202 11.11111111
203 13.88888889
204 15
205 15.55555556
206 13.88888889
207 10.55555556
208 7.222222222
209 2.222222222
210 4.444444444
211 8.888888889
212 11.11111111
213 11.11111111
214 8.888888889
215 8.333333333
216 5.555555556
217 5
218 6.666666667
219 10
220 11.66666667
221 12.77777778
222 13.88888889
223 12.77777778
224 12.22222222
225 14.44444444
226 16.11111111
227 10
228 10.55555556
229 NA
230 11.11111111
231 8.888888889
232 12.22222222
233 15.55555556
234 14.44444444
235 12.77777778
236 10
237 NA
238 NA
239 NA
240 NA
241 NA
242 NA
243 14.44444444
244 18.33333333
245 18.33333333
246 16.66666667
247 16.11111111
248 6.666666667
249 1.666666667
250 7.777777778
251 11.66666667
252 11.11111111
253 11.11111111
254 NA
255 15.55555556
256 16.11111111
257 16.11111111
258 16.11111111
259 17.22222222
260 22.22222222
261 21.11111111
262 17.22222222
263 16.11111111
264 18.33333333
265 16.11111111
266 11.11111111
267 7.777777778
268 9.444444444
269 8.888888889
270 10.55555556
271 13.33333333
272 15
273 14.44444444
274 14.44444444
275 14.44444444
276 15.55555556
277 16.11111111
278 16.11111111
279 16.11111111
280 17.22222222
281 19.44444444
282 19.44444444
283 20.55555556
284 22.22222222
285 22.22222222
286 22.22222222
287 19.44444444
288 18.33333333
289 17.22222222
290 15
291 12.22222222
292 13.33333333
293 13.88888889
294 17.77777778
295 20.55555556
296 21.11111111
297 19.44444444
298 17.22222222
299 17.22222222
300 17.22222222
301 16.11111111
302 15.55555556
303 17.77777778
304 19.44444444
305 19.44444444
306 20.55555556
307 21.66666667
308 22.22222222
309 18.88888889
310 19.44444444
311 19.44444444
312 21.11111111
313 22.22222222
314 22.77777778
315 22.77777778
316 23.88888889
317 23.33333333
318 23.33333333
319 21.11111111
320 21.11111111
321 21.11111111
322 21.11111111
323 18.88888889
324 20
325 20
326 18.88888889
327 18.33333333
328 17.77777778
329 18.88888889
330 18.88888889
331 15.55555556
332 16.66666667
333 17.77777778
334 18.88888889
335 19.44444444
336 15.55555556
337 13.88888889
338 16.11111111
339 17.22222222
340 18.33333333
341 18.33333333
342 16.11111111
343 17.77777778
344 19.44444444
345 19.44444444
346 17.77777778
347 16.11111111
348 17.77777778
349 19.44444444
350 18.88888889
351 18.33333333
352 17.22222222
353 16.66666667
354 NA
355 17.77777778
356 18.33333333
357 17.77777778
358 17.22222222
359 16.11111111
360 14.44444444
361 15
362 15.55555556
363 16.66666667
364 16.66666667
365 17.77777778
366 20")
If the problem is really just the time the information takes to be shown in the console, you could just source the file:
Save you read.table script in a different .R file, and then just do source("filepath/file.R", echo=FALSE).
But I should say that, at first glance, it doesn't look like a good idea to handle your data this way, manually copying and pasting different bits of data from different files.

Do a nonlinear least square (nls) fit for a sinusoidal model

I want to fit the following function to my data:
f(x) = Offset+Amplitudesin(FrequencyT+Phase),
or according to Wikipedia: f(x) = C+alphasin(omegaT+phi)
my data is stored in a file in two columns, and I import them by the following:
data<-read.table("C:/PATH/data.txt", header = FALSE, sep = "\t")
and convert them by the following:
minV<-data[3]
cV<-data[1]
values<-as.numeric(unlist(minV))
T<-as.numeric(unlist(cV))
Thus I get two variable of type double, one called "T" (equivalent to time T in the Equation above) and one called "values" (equivalent to the value of the function f(x) in the Equation above).
So far, everything works very fine!
However, when I want to fit f(x) to my data, I get bad results (if any). Here's my code:
r<-nls(values~C+alpha*sin(W*T+phi), start=list(C=8958.34, alpha=115.886, W=0.0652, phi=14.9286),
My problem is now, that the only thing that could be wrong in this simple four-lines-example (at least as far as I know) are the starting values. However, they are a result of a Gnuplot fit (it uses the least squares method) and they look pretty fine (as I don't have "10 reputation" I cannot attach a plot, sry). {edit1: plot attached}
Result of the code from above:
C: 8950.06571617 alpha: -23.58439668 W: 0.06416806 phi: 16.46585060
My subsequent step was to vary the starting values to get a reasonable result (perhaps the starting values are too good...) - no success!
So my question is now: Where is the problem? I wanted to use R, because I hoped, that I can produce a fit, that is a) better with b) worse starting values than Gnuplot (t least b) is a very important criteria for me, that's why I don't want to use Gnuplot).
Ps.: Yes, this is my first day of using R. So if you've got a solution/reason for me it would be nice if I could understand it.
PPs.: For those who want to get related information: I've collected a lot of links, so if i get at least "10 reputation" I can post them here. {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)
R: Nonlinear Least Squares
How to fit a curve to a sinusoidal wave
official Documentation -> Manuals
Link Roland provided in his answer
PPs.: Here the data, I used (seperated by tabstop, only column one("counter") and three ("min value") were used; I deleted some values of the last column due to maximum number characters/post {edit3: deleted additional entries, due to attached links}) {edit4: changed from "column two ("angle")" to "column one ("counter")". In the code and the answers its column one, as well.}:
#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
That kind of problem is plagued by local extrema.
By expanding the sine, you can rewrite the model as
C + alpha * sin(omega*T) + beta * cos(omega*T)
There is only one non-linear parameter left
and you can use a grid search (or some robust optimization algorithm)
on that parameter
(and linear regression for the others).
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
)
First I replaced all "," in your data with "." (alternatively you could use the dec argument of read.table), then I removed rows with less elements (those in the end) and created a proper header.
Then I read in your data using data <- read.table(text="<paste the cleaned data here>", header=TRUE).
Then I did this:
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)
And got this:
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
Then I plotted:
plot(values~T)
lines(predict(r)~T)
And got this:
Then I read this: https://stats.stackexchange.com/a/60997/11849
And did this:
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)
And got this:
And this:
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
PS: Please note that it is an extremely bad idea to call a variable T. T is an alias for TRUE in R.

Resources