Approximate a shape outline using constrained B-splines - r

I'm looking for a possibility to generate a constrained spline in order to approximate a shape (in my case, a footprint outline). As raw data, I have a table with several hundred xy-coordinate pairs, which have been collected from the boundary of the footprint. The spline should only approximate the data points (the spline does not need to pass the data points). I want to be able to smooth the spline to certain degrees. Also, I need to be able to constrain the spline: Defining several critical data points which the spline has to pass.
The R package "cobs" (COnstrained B-Splines, https://cran.r-project.org/web/packages/cobs/index.html) comes very close to providing a solution, offering parameters to constrain the spline as wanted. However, this package does not spline through an ordered sequence of data points, which of course is crucial when you want the spline to follow the boundary of a shape. I tried to spline x and y coordinates separately, but after recombining them two distinct shapes appear in the plot, so this does not seem to work (Or I got something wrong?). Is anybody aware of a solution?
Update: working example (dinosaur footprint outline)
data.txt:
structure(list(V1 = c(124.9, 86.44, 97.22, 81.34, 49.09, 57.18,
-77.6, -191.95, -284.67, -383.18, -379.27, -492.85, -547.72,
-600.67, -713.29, -814.36, -868.27, -926.99, -958.76, -1025.18,
-1077.16, -1105.07, -1126.25, -1112.77, -1087.74, -989.56, -911.59,
-859.61, -745.06, -656.5, -682.01, -637.25, -601.71, -539.09,
-394.79, -219.17, -170.17, -201.48, -122.52, -43.56, 127.97,
344.42, 539.09, 686.11, 987.63, 1253.31, 1283.15, 1536.32, 1741.14,
1832.35, 1700.3, 1787.43, 1911.31, 2017.49, 2097.81, 2135.93,
2093.73, 2066.96, 2063.78, 2022.94, 1978.69, 1919.44, 1904.03,
1895.37, 1854.22, 1810.23, 1771.09, 1741.48, 1642.45, 1553.96,
1472.96, 1396.04, 1141.65, 1085.82, 1055.02, 1358.24, 1325.94,
1031.91, 1287.14, 1265.36, 931.15, 872.12, 811.48, 755.65, 738.32,
697.41, 682.49, 647.35, 628.25, 620.09, 629.62, 675.22, 709.25,
718.78, 717.42, 551.09, 535.21, 540.98, 534.73, 546.76, 811.96,
823.03, 822.07, 607.4, 626.18, 637.73, 659.87, 756.13, 753.72,
735.91, 720.99, 676.71, 576.6, 508.26, 339.8, 179.53, 121.16,
45.6, 12.93, -9.87, -12.59, 16, 27.91, 37.78, 49.35, 8.51, 2.72,
-1.02, 59.22, 58.2, 51.73, 54.45, 0.96, 10.59, 138.62, 149.69,
144.87, 142.26, 146.34, 125.24, 124.9, 86.44, 97.22, 81.34, 49.09,
57.18, -77.6, -191.95, -284.67, -383.18, -379.27, -492.85, -547.72,
-600.67, -713.29, -814.36, -868.27, -926.99, -958.76, -1025.18,
-1077.16, -1105.07, -1126.25, -1112.77, -1087.74, -989.56, -911.59,
-859.61, -745.06, -656.5, -682.01, -637.25, -601.71, -539.09,
-394.79, -219.17, -170.17, -201.48, -122.52, -43.56, 127.97,
344.42, 539.09, 686.11, 987.63, 1253.31, 1283.15, 1536.32, 1741.14,
1832.35, 1700.3, 1787.43, 1911.31, 2017.49, 2097.81, 2135.93,
2093.73, 2066.96, 2063.78, 2022.94, 1978.69, 1919.44, 1904.03,
1895.37, 1854.22, 1810.23, 1771.09, 1741.48, 1642.45, 1553.96,
1472.96, 1396.04, 1141.65, 1085.82, 1055.02, 1358.24, 1325.94,
1031.91, 1287.14, 1265.36, 931.15, 872.12, 811.48, 755.65, 738.32,
697.41, 682.49, 647.35, 628.25, 620.09, 629.62, 675.22, 709.25,
718.78, 717.42, 551.09, 535.21, 540.98, 534.73, 546.76, 811.96,
823.03, 822.07, 607.4, 626.18, 637.73, 659.87, 756.13, 753.72,
735.91, 720.99, 676.71, 576.6, 508.26, 339.8, 179.53, 121.16,
45.6, 12.93, -9.87, -12.59, 16, 27.91, 37.78, 49.35, 8.51, 2.72,
-1.02, 59.22, 58.2, 51.73, 54.45, 0.96, 10.59, 138.62, 149.69,
144.87, 142.26, 146.34, 125.24), V2 = c(-446.8, -415.83, -394.43,
-259.19, -104.69, -4.03, 58.59, -80.26, 52.11, -48.33, -142.23,
-176.89, -233.68, -321.28, -416.57, -457.97, -458.93, -429.09,
-422.35, -450.27, -431.98, -379.03, -260.63, -123.94, -2.65,
269.76, 455.55, 548.92, 616.3, 691.38, 756.84, 888.72, 1016.97,
1157.18, 1198.02, 1101.37, 1025.14, 929.84, 852.25, 766.48, 717.47,
733.81, 784.18, 835.91, 1225.63, 1198.68, 925.3, 742.4, 814.13,
732.45, 586.79, 394.84, 212.42, 28.64, -111.58, -337.56, -490.03,
-526.07, -528.82, -547.2, -551.97, -552.3, -585.51, -551.34,
-543.16, -526.1, -494.11, -466.88, -355.93, -274.94, -215.04,
-114.3, -194.21, -103.73, -3.62, 104.2, 230.8, 154.25, 380.55,
416.62, 260.07, 295.75, 295.75, 251.47, 220.67, 225.96, 180.72,
121.52, 4.14, -127.23, -176.24, -332.11, -408.35, -494.11, -573.75,
-582.62, -678.88, -730.38, -788.62, -831.94, -846.38, -895.95,
-934.46, -968.15, -1033.12, -1097.62, -1150.08, -1157.3, -1254.04,
-1340.2, -1441.75, -1500.47, -1550.52, -1605.39, -1681.44, -1709.84,
-1715.22, -1672.34, -1607, -1522.59, -1440.57, -1421.18, -1345.62,
-1247.95, -1190.77, -1181.58, -1071.65, -1037.62, -1010.39, -998.82,
-986.57, -937.9, -887.29, -842.05, -831.46, -774.66, -703.91,
-573.75, -533.59, -448.16, -446.8, -415.83, -394.43, -259.19,
-104.69, -4.03, 58.59, -80.26, 52.11, -48.33, -142.23, -176.89,
-233.68, -321.28, -416.57, -457.97, -458.93, -429.09, -422.35,
-450.27, -431.98, -379.03, -260.63, -123.94, -2.65, 269.76, 455.55,
548.92, 616.3, 691.38, 756.84, 888.72, 1016.97, 1157.18, 1198.02,
1101.37, 1025.14, 929.84, 852.25, 766.48, 717.47, 733.81, 784.18,
835.91, 1225.63, 1198.68, 925.3, 742.4, 814.13, 732.45, 586.79,
394.84, 212.42, 28.64, -111.58, -337.56, -490.03, -526.07, -528.82,
-547.2, -551.97, -552.3, -585.51, -551.34, -543.16, -526.1, -494.11,
-466.88, -355.93, -274.94, -215.04, -114.3, -194.21, -103.73,
-3.62, 104.2, 230.8, 154.25, 380.55, 416.62, 260.07, 295.75,
295.75, 251.47, 220.67, 225.96, 180.72, 121.52, 4.14, -127.23,
-176.24, -332.11, -408.35, -494.11, -573.75, -582.62, -678.88,
-730.38, -788.62, -831.94, -846.38, -895.95, -934.46, -968.15,
-1033.12, -1097.62, -1150.08, -1157.3, -1254.04, -1340.2, -1441.75,
-1500.47, -1550.52, -1605.39, -1681.44, -1709.84, -1715.22, -1672.34,
-1607, -1522.59, -1440.57, -1421.18, -1345.62, -1247.95, -1190.77,
-1181.58, -1071.65, -1037.62, -1010.39, -998.82, -986.57, -937.9,
-887.29, -842.05, -831.46, -774.66, -703.91, -573.75, -533.59,
-448.16)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA,
-280L))
require(cobs)
xy <- dget(data.txt)
#Cumchord function (from Claude, 2008): Cumulative chordal distance vector
cumchord<-function(M)
{cumsum(sqrt(apply((M-rbind(M[1,],
M[-(dim(M)[1]),]))^2,1,sum)))}
z <- cumchord(xy)
#Calculating B-spline for x and y values separately
x <- cobs(z,xy[,1],nknots=50)
y <- cobs(z,xy[,2],nknots=50)
#Plot spline
plot(xy)
lines(x$fitted,y$fitted)
Image of resulting plot

Following the comments thread, here are some graphs. I use Momocs since I'm familiar with it and it will shorten examples.
I brief, your problem is that there are two outlines in your outline.
I also include original use of spline by Julien Claude, and two additional examples with bezier curves and elliptic Fourier transforms. All 4 can be used to describe an outline (and reconstruct it) and it's probably worth gathering them here.
The picture gathers the original shape and these 4 methods
Now the code. It's not particularly long but quite repetitive.
# devtools::install_github("vbonhomme/Momocs")
library(Momocs) # version 1.0.3
xy <- structure(list(
V1 = c(124.9, 86.44, 97.22, 81.34, 49.09, 57.18, -77.6, -191.95, -284.67, -383.18, -379.27, -492.85, -547.72, -600.67, -713.29, -814.36, -868.27, -926.99, -958.76, -1025.18, -1077.16, -1105.07, -1126.25, -1112.77, -1087.74, -989.56, -911.59, -859.61, -745.06, -656.5, -682.01, -637.25, -601.71, -539.09, -394.79, -219.17, -170.17, -201.48, -122.52, -43.56, 127.97, 344.42, 539.09, 686.11, 987.63, 1253.31, 1283.15, 1536.32, 1741.14, 1832.35, 1700.3, 1787.43, 1911.31, 2017.49, 2097.81, 2135.93, 2093.73, 2066.96, 2063.78, 2022.94, 1978.69, 1919.44, 1904.03, 1895.37, 1854.22, 1810.23, 1771.09, 1741.48, 1642.45, 1553.96, 1472.96, 1396.04, 1141.65, 1085.82, 1055.02, 1358.24, 1325.94, 1031.91, 1287.14, 1265.36, 931.15, 872.12, 811.48, 755.65, 738.32, 697.41, 682.49, 647.35, 628.25, 620.09, 629.62, 675.22, 709.25, 718.78, 717.42, 551.09, 535.21, 540.98, 534.73, 546.76, 811.96, 823.03, 822.07, 607.4, 626.18, 637.73, 659.87, 756.13, 753.72, 735.91, 720.99, 676.71, 576.6, 508.26, 339.8, 179.53, 121.16, 45.6, 12.93, -9.87, -12.59, 16, 27.91, 37.78, 49.35, 8.51, 2.72, -1.02, 59.22, 58.2, 51.73, 54.45, 0.96, 10.59, 138.62, 149.69, 144.87, 142.26, 146.34, 125.24, 124.9, 86.44, 97.22, 81.34, 49.09, 57.18, -77.6, -191.95, -284.67, -383.18, -379.27, -492.85, -547.72, -600.67, -713.29, -814.36, -868.27, -926.99, -958.76, -1025.18, -1077.16, -1105.07, -1126.25, -1112.77, -1087.74, -989.56, -911.59, -859.61, -745.06, -656.5, -682.01, -637.25, -601.71, -539.09, -394.79, -219.17, -170.17, -201.48, -122.52, -43.56, 127.97, 344.42, 539.09, 686.11, 987.63, 1253.31, 1283.15, 1536.32, 1741.14, 1832.35, 1700.3, 1787.43, 1911.31, 2017.49, 2097.81, 2135.93, 2093.73, 2066.96, 2063.78, 2022.94, 1978.69, 1919.44, 1904.03, 1895.37, 1854.22, 1810.23, 1771.09, 1741.48, 1642.45, 1553.96, 1472.96, 1396.04, 1141.65, 1085.82, 1055.02, 1358.24, 1325.94, 1031.91, 1287.14, 1265.36, 931.15, 872.12, 811.48, 755.65, 738.32, 697.41, 682.49, 647.35, 628.25, 620.09, 629.62, 675.22, 709.25, 718.78, 717.42, 551.09, 535.21, 540.98, 534.73, 546.76, 811.96, 823.03, 822.07, 607.4, 626.18, 637.73, 659.87, 756.13, 753.72, 735.91, 720.99, 676.71, 576.6, 508.26, 339.8, 179.53, 121.16, 45.6, 12.93, -9.87, -12.59, 16, 27.91, 37.78, 49.35, 8.51, 2.72, -1.02, 59.22, 58.2, 51.73, 54.45, 0.96, 10.59, 138.62, 149.69, 144.87, 142.26, 146.34, 125.24),
V2 = c(-446.8, -415.83, -394.43, -259.19, -104.69, -4.03, 58.59, -80.26, 52.11, -48.33, -142.23, -176.89, -233.68, -321.28, -416.57, -457.97, -458.93, -429.09, -422.35, -450.27, -431.98, -379.03, -260.63, -123.94, -2.65, 269.76, 455.55, 548.92, 616.3, 691.38, 756.84, 888.72, 1016.97, 1157.18, 1198.02, 1101.37, 1025.14, 929.84, 852.25, 766.48, 717.47, 733.81, 784.18, 835.91, 1225.63, 1198.68, 925.3, 742.4, 814.13, 732.45, 586.79, 394.84, 212.42, 28.64, -111.58, -337.56, -490.03, -526.07, -528.82, -547.2, -551.97, -552.3, -585.51, -551.34, -543.16, -526.1, -494.11, -466.88, -355.93, -274.94, -215.04, -114.3, -194.21, -103.73, -3.62, 104.2, 230.8, 154.25, 380.55, 416.62, 260.07, 295.75, 295.75, 251.47, 220.67, 225.96, 180.72, 121.52, 4.14, -127.23, -176.24, -332.11, -408.35, -494.11, -573.75, -582.62, -678.88, -730.38, -788.62, -831.94, -846.38, -895.95, -934.46, -968.15, -1033.12, -1097.62, -1150.08, -1157.3, -1254.04, -1340.2, -1441.75, -1500.47, -1550.52, -1605.39, -1681.44, -1709.84, -1715.22, -1672.34, -1607, -1522.59, -1440.57, -1421.18, -1345.62, -1247.95, -1190.77, -1181.58, -1071.65, -1037.62, -1010.39, -998.82, -986.57, -937.9, -887.29, -842.05, -831.46, -774.66, -703.91, -573.75, -533.59, -448.16, -446.8, -415.83, -394.43, -259.19, -104.69, -4.03, 58.59, -80.26, 52.11, -48.33, -142.23, -176.89, -233.68, -321.28, -416.57, -457.97, -458.93, -429.09, -422.35, -450.27, -431.98, -379.03, -260.63, -123.94, -2.65, 269.76, 455.55, 548.92, 616.3, 691.38, 756.84, 888.72, 1016.97, 1157.18, 1198.02, 1101.37, 1025.14, 929.84, 852.25, 766.48, 717.47, 733.81, 784.18, 835.91, 1225.63, 1198.68, 925.3, 742.4, 814.13, 732.45, 586.79, 394.84, 212.42, 28.64, -111.58, -337.56, -490.03, -526.07, -528.82, -547.2, -551.97, -552.3, -585.51, -551.34, -543.16, -526.1, -494.11, -466.88, -355.93, -274.94, -215.04, -114.3, -194.21, -103.73, -3.62, 104.2, 230.8, 154.25, 380.55, 416.62, 260.07, 295.75, 295.75, 251.47, 220.67, 225.96, 180.72, 121.52, 4.14, -127.23, -176.24, -332.11, -408.35, -494.11, -573.75, -582.62, -678.88, -730.38, -788.62, -831.94, -846.38, -895.95, -934.46, -968.15, -1033.12, -1097.62, -1150.08, -1157.3, -1254.04, -1340.2, -1441.75, -1500.47, -1550.52, -1605.39, -1681.44, -1709.84, -1715.22, -1672.34, -1607, -1522.59, -1440.57, -1421.18, -1345.62, -1247.95, -1190.77, -1181.58, -1071.65, -1037.62, -1010.39, -998.82, -986.57, -937.9, -887.29, -842.05, -831.46, -774.66, -703.91, -573.75, -533.59, -448.16)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, -280L))
### First thing first: double outline ---------------------
coo_plot(xy)
ldk_labels(xy) # blurry since superimposed
coo_plot(xy[1:140, ], lwd=3) # first shape
coo_draw(xy[-(1:140), ], border="white") # second shape
# so from here, we will use the first 140th points from xy and name it 'shp' to avoid confusion
# if you're bored with dinos footprints, you can use beer bottles with shp <- bot[9] for a guinness
shp <- xy[1:140, ]
### 1.Natural splines ---------------------
shp_cumchord <- coo_perimcum(shp) # cumchord equivalent
shp_spline <- cbind(spline(shp_cumchord, shp[, 1], method="natural", n=120)$y,
spline(shp_cumchord, shp[, 2], method="natural", n=120)$y)
coo_plot(shp, main="natural spline", zoom=1.2)
coo_draw(shp_spline, border="blue", lwd=2)
### 2. B-splines with cobs ---------------------
library(cobs)
shp_bspline <- cbind(cobs(shp_cumchord, shp[, 1], nknots=50)$fitted,
cobs(shp_cumchord, shp[, 2], nknots=50)$fitted)
coo_plot(shp, main = "bspline", zoom=1.2)
coo_draw(shp_bspline, border="blue")
### 3. Bezier curves ---------------------
# built in function so it's shorter
shp_bezier <- shp %>% bezier() %>% bezier_i()
coo_plot(shp, main = "bezier", zoom=1.2)
coo_draw(shp_bezier, border="blue")
### 4. elliptic Fourier transforms ---------------------
# another built in function
shp_eft <- shp %>% efourier() %>% efourier_i()
coo_plot(shp, main = "bspline", zoom=1.2)
coo_draw(shp_eft, border="blue")
### 5. A panel of original shape and 4 methods ---------
Out(list(original=shp,
nat_spline=shp_spline, bspline=shp_bspline,
bezier=shp_bezier, eft=shp_eft)) %>%
panel(names=TRUE, dim=c(1, 5))

Related

How to visualize a multiple linear regression model?

Does anyone know why this multiple regression model's visualization fails?
This is not a syntax error or typo. I think I missed something, but don't know what's the missing part.
> Oct_Mar %>% model(lm(ctr ~ view + price_view + cart + price_cart +
> purchase + price_purchase)) %>% forecast(new_data(Oct_Mar, 30) %>%
> autoplot(Oct_Mar)
Error throws as below
#MULTIPLE LINEAR REGRESSION MODEL
##############################################################
library(stats)
library(tidyverse)
#Da es sich bei den Daten des ausgewählten Datenset um Panel Data handelt nutze ich das Multiple Regression Model lm_Oct_Mar <- lm(ctr ~ view + price_view + cart + price_cart + purchase + price_purchase, data=Oct_Mar) summary(lm_Oct_Mar)
Fit.consMR <- lm(ctr ~ view + price_view + cart + price_cart + purchase + price_purchase, data=Oct_Mar)
#ctr=1.844e-02 + -7.330e-09*view + 1.010e-07*price_view + 1.639e-088*cart + -2.222e-07*price_cart + 3.302e-07*purchase + -4.157e-07*price_purchase
```
structure(list(date = c("2019.10.01", "2019.10.02", "2019.10.03",
"2019.10.04", "2019.10.05", "2019.10.06", "2019.10.07", "2019.10.08",
"2019.10.09", "2019.10.10", "2019.10.11", "2019.10.12", "2019.10.13",
"2019.10.14", "2019.10.15", "2019.10.16", "2019.10.17", "2019.10.18",
"2019.10.19", "2019.10.20", "2019.10.21", "2019.10.22", "2019.10.23",
"2019.10.24", "2019.10.25", "2019.10.26", "2019.10.27", "2019.10.28",
"2019.10.29", "2019.10.30", "2019.10.31", "2019.11.01", "2019.11.02",
"2019.11.03", "2019.11.04", "2019.11.05", "2019.11.06", "2019.11.07",
"2019.11.08", "2019.11.09", "2019.11.10", "2019.11.11", "2019.11.12",
"2019.11.13", "2019.11.14", "2019.11.15", "2019.11.16", "2019.11.17",
"2019.11.18", "2019.11.19", "2019.11.20", "2019.11.21", "2019.11.22",
"2019.11.23", "2019.11.24", "2019.11.25", "2019.11.26", "2019.11.27",
"2019.11.28", "2019.11.29", "2019.11.30", "2019.12.01", "2019.12.02",
"2019.12.03", "2019.12.04", "2019.12.05", "2019.12.06", "2019.12.07",
"2019.12.08", "2019.12.09", "2019.12.10", "2019.12.11", "2019.12.12",
"2019.12.13", "2019.12.14", "2019.12.15", "2019.12.16", "2019.12.17",
"2019.12.18", "2019.12.19", "2019.12.20", "2019.12.21", "2019.12.22",
"2019.12.23", "2019.12.24", "2019.12.25", "2019.12.26", "2019.12.27",
"2019.12.28", "2019.12.29", "2019.12.30", "2019.12.31", "2020.01.01",
"2020.01.02", "2020.01.03", "2020.01.04", "2020.01.05", "2020.01.06",
"2020.01.07", "2020.01.08", "2020.01.09", "2020.01.10", "2020.01.11",
"2020.01.12", "2020.01.13", "2020.01.14", "2020.01.15", "2020.01.16",
"2020.01.17", "2020.01.18", "2020.01.19", "2020.01.20", "2020.01.21",
"2020.01.22", "2020.01.23", "2020.01.24", "2020.01.25", "2020.01.26",
"2020.01.27", "2020.01.28", "2020.01.29", "2020.01.30", "2020.01.31",
"2020.02.01", "2020.02.02", "2020.02.03", "2020.02.04", "2020.02.05",
"2020.02.06", "2020.02.07", "2020.02.08", "2020.02.09", "2020.02.10",
"2020.02.11", "2020.02.12", "2020.02.13", "2020.02.14", "2020.02.15",
"2020.02.16", "2020.02.17", "2020.02.18", "2020.02.19", "2020.02.20",
"2020.02.21", "2020.02.22", "2020.02.23", "2020.02.24", "2020.02.25",
"2020.02.26", "2020.02.27", "2020.02.28", "2020.02.29", "2020.03.01",
"2020.03.02", "2020.03.03", "2020.03.04", "2020.03.05", "2020.03.06",
"2020.03.07", "2020.03.08", "2020.03.09", "2020.03.10", "2020.03.11",
"2020.03.12", "2020.03.13", "2020.03.14", "2020.03.15", "2020.03.16",
"2020.03.17", "2020.03.18", "2020.03.19", "2020.03.20", "2020.03.21",
"2020.03.22", "2020.03.23", "2020.03.24", "2020.03.25", "2020.03.26",
"2020.03.27", "2020.03.28", "2020.03.29", "2020.03.30", "2020.03.31"
), price_view = c(35.79, 180.16, 437.57, 10.3, 74.26, 79.8, 89.84,
121.24, 461.95, 142.06, 241.71, 52, 43.24, 41.16, 167.05, 764.06,
91.64, 189.82, 38.59, 152.64, 86.23, 321.33, 411.83, 256.88,
352.39, 76.32, 360.11, 123.53, 43.41, 149.38, 14.16, 489.07,
1661.74, 1253.07, 25.71, 154.42, 990.89, 1645.93, 144.12, 84.43,
240.25, 148.18, 41.13, 262.56, 168.78, 860.85, 239.31, 372.98,
165.64, 134.32, 20.7, 43.73, 765.76, 51.48, 599.49, 893.79, 155.29,
334.37, 46.82, 1814.72, 196.27, 1302.48, 40.16, 1161.68, 381.48,
184.48, 48.91, 221.11, 434.73, 149.27, 77.22, 882.49, 106.05,
669.23, 282.86, 179.67, 12.97, 460.24, 38.59, 278.26, 243.76,
1904.79, 84.93, 32.18, 25.71, 496.54, 29.6, 1466.83, 164.33,
234.76, 19.95, 308.37, 1130.02, 7.47, 79.8, 65.9, 746.45, 1347.78,
1270.82, 69.42, 231.41, 195.6, 715.33, 208.47, 720.46, 414.68,
24.45, 217.82, 434.45, 483.92, 1500.42, 318.15, 339.29, 267.45,
133.85, 9.03, 11.81, 280.57, 916.74, 58.51, 339.78, 33.98, 263.58,
19.31, 239.88, 489.07, 84.92, 344.9, 95.24, 99.1, 142.58, 480.58,
104.74, 14.83, 252, 1039.41, 28.3, 328.97, 341.55, 278.26, 43.73,
91.35, 102.32, 131.25, 155.15, 77.74, 14.67, 132.63, 1185.36,
1145.975, 1106.59, 849.42, 117.63, 171.32, 167.31, 252.23, 248.14,
111.15, 257.15, 27.62, 169.86, 101.89, 282.89, 298.57, 86.49,
196.32, 1415.45, 898.35, 334.6, 17.99, 13.62, 566.27, 60.41,
36.34, 62.04, 308.81, 32.95, 127.44, 836.57, 221.34, 360.34,
159.31, 20.57), view = c(1206151, 1152770, 1087372, 1344804,
1270060, 1262993, 1159265, 1323522, 1301376, 1240347, 1445162,
1432321, 1583572, 1376274, 1462409, 1443323, 1337174, 1413405,
1382403, 1443838, 1342668, 1353053, 1318395, 1252747, 1369922,
1288939, 1330209, 1220710, 1187883, 1169955, 1207854, 1402754,
1513400, 1524803, 1743304, 1670637, 1644359, 1748812, 1789808,
1783142, 1845552, 1907417, 1892753, 1920411, 2864410, 5691766,
5986292, 5759703, 1905351, 1627672, 1598554, 1573101, 1471242,
1474138, 1500022, 1496128, 1557252, 1547199, 1560191, 1727852,
1644405, 1706901, 1629904, 1547658, 1468085, 1540157, 1652208,
1725106, 1724452, 1627222, 1651328, 1605421, 1650612, 1634861,
1760750, 2167056, 2875847, 2780816, 2665285, 2528244, 2387520,
2340327, 2471739, 2372930, 2326654, 2322753, 2240514, 2058141,
2089081, 2474226, 2294820, 1603749, 1427733, 1700904, 1765457,
1754424, 1738774, 1696188, 1701769, 1585870, 1556542, 1557542,
1618230, 1645866, 1627433, 1612956, 1555416, 1773179, 1826768,
2021676, 2104199, 1801073, 1733142, 1593991, 1645225, 1557626,
1637470, 1721003, 1545472, 1594688, 1565742, 1651606, 1999670,
2217825, 1985751, 1680034, 1608904, 1620473, 1628906, 1726835,
1589058, 1714745, 1751044, 1896265, 2429526, 2268487, 1935249,
1916034, 2239698, 1916650, 1981570, 1948648, 1987134, 1749514,
1822349, 1830307, 1748590, 1734610, 1798308, 1399256, 1000204,
1257475, 1770064, 2416707, 2477258, 2487470, 2457500, 2210539,
2377633, 2026050, 2301337, 2218894, 2012789, 1700619, 1481115,
1562027, 1560348, 1338829, 1244973, 1142989, 1260747, 1316975,
1387394, 1319559, 1440470, 1451015, 1439649, 1390411, 1336076,
1369834, 1255626, 1244163, 1283731), price_cart = c(29.51, 1415.48,
99.86, 358.57, 617.51, 1052.79, 1747.79, 190.56, 128.28, 252.38,
250.91, 720.48, 33.42, 643, 191.77, 460.11, 408.5, 789.9, 577.94,
49.36, 380.7, 19.56, 994.86, 756.71, 223.66, 437.33, 1684.28,
366.16, 968.34, 1683.07, 550.77, 503.09, 29.09, 179.67, 210.62,
22.66, 131.66, 68.96, 360.06, 494.22, 1023.62, 1569.92, 28.29,
694.97, 127.05, 37.85, 282.89, 178.9, 913.28, 1022.42, 424.7,
573.7, 1029.34, 30.12, 20.82, 17.99, 107.53, 41.19, 85.82, 1002.55,
140.98, 167.03, 231.67, 25.71, 205.64, 30.81, 51.22, 65.9, 7.08,
308.63, 227.79, 16.22, 7.89, 62.52, 48.88, 586.63, 602.07, 1312.26,
128.32, 179.9, 849.42, 100.9, 1284.2, 12.84, 128.42, 59.18, 176.99,
38.02, 48.88, 694.54, 262.3, 1402.84, 1453.18, 3.84, 453.01,
76.93, 7.04, 865.93, 865.4, 40.75, 1423.07, 1534.66, 679.27,
11.25, 102.63, 436.3, 853.93, 694.97, 850.47, 477.49, 1234.97,
10.27, 23.94, 643.23, 89.84, 290.34, 320.99, 6.44, 140.28, 188.89,
56.88, 1326.31, 194.34, 140.28, 771.96, 140.03, 20.21, 1464.39,
59.18, 57.92, 1156.81, 50.43, 300.12, 38.1, 832.71, 57.91, 174.5,
100.36, 248.14, 109.34, 100.7, 242.7, 266.67, 592.01, 242.18,
22.66, 566.04, 38.61, 812.06, 490.33, 168.6, 172.03, 49.91, 16.73,
108.04, 347.47, 97.79, 111.15, 514.79, 126.1, 178.87, 870.03,
529.31, 43.5, 2110.48, 771.94, 15.32, 105.25, 7.14, 312.67, 61.75,
165.51, 48.37, 643.49, 303.48, 35.78, 154.42, 209.71, 76.69,
25.46, 1415.45, 123.53, 602.31), cart = c(16658, 17268, 19323,
43826, 35493, 32145, 18052, 18442, 18432, 18997, 21450, 20691,
24833, 44821, 49513, 45272, 40368, 40127, 39455, 40533, 36675,
36945, 36407, 35721, 36800, 34776, 34256, 17838, 17455, 16996,
16798, 18911, 19350, 20211, 21960, 19231, 19670, 19446, 77319,
70093, 71585, 75135, 69669, 71613, 170183, 481862, 405584, 426261,
83117, 72450, 72311, 75530, 70171, 64801, 68099, 71405, 71622,
71324, 71504, 92345, 81760, 84473, 80869, 70192, 66718, 71048,
83618, 84231, 80773, 80675, 81420, 78947, 80162, 82360, 86689,
109721, 183764, 155406, 146906, 137487, 127900, 124577, 127381,
126700, 124797, 127554, 123966, 120940, 127769, 148663, 148608,
119062, 57614, 71342, 95608, 80629, 78782, 79099, 77396, 74671,
72772, 74827, 72221, 73406, 72999, 71182, 70235, 79414, 104791,
103481, 102597, 94354, 90666, 83642, 83223, 73075, 73582, 73849,
70067, 71600, 72179, 130757, 208231, 169156, 137970, 116560,
104701, 102836, 101145, 101605, 90864, 92635, 95114, 100283,
158447, 131720, 118661, 126405, 132399, 98277, 96270, 95284,
96886, 89046, 91384, 89585, 83771, 83241, 84151, 62362.5, 40574,
48944, 81869, 146953, 135144, 132819, 134255, 131648, 141696,
122204, 122752, 120927, 112159, 102239, 95998, 97600, 99032,
79662, 76622, 69585, 73822, 74488, 75621, 69098, 73761, 76429,
75664, 77671, 77090, 77835, 68888, 69091, 73986), price_purchase = c(130.76,
419.6, 251.74, 252.88, 64.02, 272.59, 172.72, 88.81, 28.73, 1003.86,
346.47, 130.48, 29.86, 280.11, 358.57, 385.83, 287.61, 22.95,
58.08, 854.08, 28.28, 62.91, 994.86, 51.22, 9.01, 77.21, 244.15,
366.16, 366.8, 213.25, 35.52, 566.3, 35.78, 1106.82, 64.35, 722.18,
131.66, 166.1, 823.9, 138.23, 334.6, 328.19, 243.51, 488.8, 159.57,
106.8, 54.03, 27, 308.63, 1022.42, 463.31, 144.66, 44.53, 25.48,
126.18, 365.52, 133.92, 97.27, 12.84, 1002.55, 107.41, 132.31,
131.2, 789.57, 230.2, 12.36, 229.86, 1386.91, 154.19, 18.19,
76.96, 882.49, 191.55, 46.08, 24.17, 102.65, 326.62, 924.06,
923.73, 88.29, 41.16, 128.42, 326.88, 137.96, 30.68, 108.88,
181.19, 241.34, 128.32, 137.46, 1279.81, 643.23, 1275.16, 717.245,
159.33, 745.37, 288.27, 177.26, 168.58, 66.85, 331.51, 437.31,
643.23, 9.3, 0.85, 436.3, 105.51, 7.7, 79.44, 1321.37, 160.89,
107.21, 172.25, 514.79, 141.06, 900.64, 153.22, 924.4, 176.34,
94.98, 162.6, 1326.25, 39.9, 38.15, 2162.22, 180.95, 153.41,
720.48, 720.48, 15.42, 140.28, 514.02, 720.47, 174.7, 197.69,
411.08, 741.07, 230.12, 501.89, 109.34, 643.26, 23.17, 242.48,
1317.36, 69.76, 178.11, 153.55, 32.18, 812.06, 482.7, 153.34,
172.03, 128.68, 939.54, 108.04, 165.89, 56.63, 43.76, 171.17,
98.59, 21.95, 280.28, 181.47, 730.01, 159.31, 60.75, 31.15, 1412.39,
7.14, 942.84, 321.06, 165.51, 284.95, 169.42, 303.48, 224.3,
416.43, 385.85, 492.08, 334.6, 1415.45, 123.53, 308.55), purchase = c(19307,
19469, 19255, 27041, 23494, 22171, 21378, 23072, 22748, 21993,
26224, 25373, 29561, 28405, 26372, 31394, 28318, 25850, 24657,
25098, 25167, 25385, 24731, 23999, 23929, 22653, 23403, 21112,
20374, 20817, 20099, 22458, 21864, 22145, 26889, 24875, 25319,
24863, 25714, 22768, 22878, 24931, 22725, 22548, 22124, 45185.5,
68247, 185195, 28537, 24967, 24947, 25266, 24187, 22243, 23163,
24827, 24226, 24443, 24305, 32107, 28178, 28345, 28548, 24358,
24473, 25469, 27505, 27012, 25766, 26802, 27059, 25906, 26044,
26712, 26559, 35077, 63796, 51899, 49578, 48212, 46405, 44255,
44719, 46602, 44917, 44949, 44154, 43081, 45287, 49597, 50729,
38233, 3574, 13975.5, 24377, 28938, 28427, 28875, 27722, 27510,
26492, 27481, 26059, 25869, 27525, 26322, 27121, 29614, 35086,
32884, 32548, 32619, 31698, 30089, 30398, 27579, 26662, 26880,
27052, 26841, 27403, 27612, 33750, 32536, 32308, 28645, 27652,
28276, 28533, 28426, 25379, 26027, 46480, 60013, 102117, 83216,
76048, 72365, 87586, 63260, 36377, 31438, 31258, 29324, 28946,
29017, 27884, 28063, 27809, 27649.5, 27490, 26316, 31358, 55087,
46356, 45228, 44406, 43521, 45501, 40281, 41091, 41681, 39266,
36268, 34754, 35341, 35943, 28852, 27810, 25186, 25501, 26232,
25775, 23698, 25314, 25960, 26259, 27487, 26966, 25817, 21294,
22704, 23997), ctr = c(0.0157890561813006, 0.0166396305077271,
0.0173986509381537, 0.0194731497951218, 0.0179954394804347, 0.01711863909483,
0.0181582360570687, 0.0171927115779559, 0.0172358403646591, 0.0174638541971058,
0.0178806664612045, 0.017462347179514, 0.0183790774089859, 0.0199881077619723,
0.0174426987635606, 0.021089685240109, 0.0205569049800296, 0.0177842661874661,
0.0173413941476575, 0.0169081718788632, 0.0182456430344012, 0.0182626162052032,
0.0182543279386951, 0.0186259961442581, 0.0170104683085926, 0.0171132003490177,
0.0171517774365777, 0.0170457664943143, 0.016903142521019, 0.0175382134561578,
0.0164120092891695, 0.0157969704536582, 0.014264557168488, 0.0143332034531726,
0.0152322825367764, 0.0147200846456646, 0.0152154800186776, 0.0140607309566817,
0.0137719608789332, 0.0122855439272407, 0.0119334194687182, 0.0125752060979989,
0.01158007808718, 0.0113191407332442, 0.00729059877222415, 0.00731911608538772,
0.0106771470535411, 0.029937936916542, 0.0143512493034839, 0.0146854166936255,
0.0149305898441825, 0.015325442746133, 0.015691446743994, 0.0144534643673336,
0.0147711815606066, 0.0158382630541111, 0.0148728508159624, 0.015102040564144,
0.0148955533969277, 0.0176392994824187, 0.0163240478169816, 0.0158230497930639,
0.0166871934499785, 0.0150557839107457, 0.0159453688844757, 0.0158074236363467,
0.0158454822084702, 0.0149292254566175, 0.0142730130593139, 0.0156929838274791,
0.0156162350209032, 0.0153802494466767, 0.0150476029799385, 0.015555365325721,
0.0143761174252573, 0.0154064275947974, 0.0208510166815324, 0.0176754346231314,
0.0176296702464377, 0.01808584587117, 0.0184482114318881, 0.0179540460804964,
0.0172054387638893, 0.0186435592467685, 0.0183226179107802, 0.0183442319676677,
0.0186738733252132, 0.0197702609494553, 0.0204285359857455, 0.0189093019186096,
0.0207614056972417, 0.0221922195760301, 0.00240617175649865,
0.00788575626634226, 0.01309841408011, 0.0157695717780358, 0.0156402333683254,
0.0162649757475833, 0.0155814665868539, 0.0165668899473124, 0.0162596037350689,
0.0168350415867981, 0.0154154128099543, 0.0150464847912372, 0.0161870630522126,
0.0156293605393382, 0.0166831626222356, 0.0159851624182969, 0.0181646017543342,
0.0154736802975027, 0.0147489845006063, 0.0172093148404027, 0.0173801189598905,
0.0179353887292393, 0.017586875624838, 0.0169123585500959, 0.0155822266067893,
0.0149761651657073, 0.0167448758587691, 0.0161082597966258, 0.0167303551270177,
0.0154917937591837, 0.015286011465188, 0.0136306070303869, 0.0152129210946259,
0.0159440585908669, 0.0161367409642245, 0.0164079686231546, 0.016492577386447,
0.0155465861608803, 0.0151072490270382, 0.0144004027929932, 0.0251766100192941,
0.0300583807652007, 0.0394582941939502, 0.0346703430162482, 0.0370259651104479,
0.0354306787130485, 0.0369234479028471, 0.0313956783546004, 0.0175071227813499,
0.0153811379243536, 0.0149988963637585, 0.0159494386911496, 0.0151254119566314,
0.0151138709885764, 0.0152175253675449, 0.015437458845637, 0.0147726989007463,
0.02059282, 0.0264129334017437, 0.0201436139554002, 0.0169325780144314,
0.0214876387664511, 0.0177445890793224, 0.0172606914733451, 0.017133563936406,
0.0185813515317095, 0.0180607614170281, 0.0187505760492009, 0.0169511102933927,
0.0178137558385877, 0.0184785698285323, 0.0201169476464591, 0.0220364679005246,
0.0212945438945016, 0.021660499704709, 0.0203399246100257, 0.021042755155702,
0.0207706911083365, 0.0191080416224264, 0.0188521002714409, 0.0176177277744931,
0.0170654092407268, 0.0167173964870618, 0.0169957130997929, 0.0173290930652611,
0.018723068602435, 0.0190819762151085, 0.0178334964691514, 0.0160768402598991,
0.0172883539665594, 0.0176745227466401)), row.names = c(NA, -183L
), class = "data.frame")

R: Error when plotting a normal to compare with my data distribution

I have the following vector called Datadiff:
[1] 0.00000 -0.01415 2.68350 -4.26980 -2.28975 -1.27000 6.63400 -3.13965 -1.67050 -0.28920 -0.42435
[12] -1.36785 -1.97000 6.62200 -0.82470 -1.99530 1.84275 -4.70575 3.67150 -1.00900 -1.00205 -0.77350
[23] -0.86150 -2.12430 2.20255 2.03850 -0.19295 1.83205 2.04300 -3.51635 -3.32000 9.07800 -0.60280
[34] 11.15045 -8.44820 -9.49590 0.13885 -1.06000 3.85800 -1.27255 0.35010 3.38085 -5.70870 5.08605
[45] -6.67000 6.57600 -4.48095 0.05825 3.35215 5.94525 -8.91095 -1.21000 7.99800 -2.21730 13.66610
[56] -20.52355 0.68220 3.64315 -0.46000 4.08800 5.76705 3.88775 -12.32920 -0.08525 -4.10745 -6.70000
[67] 8.73800 5.17880 -8.20225 -1.61770 23.66225 -16.23860 -2.25000 -5.24400 8.21195 19.02100 -1.55330
[78] -10.74680 -13.19870 4.32000 -3.45600 0.29670 2.22515 -1.77935 -0.26525 0.32630 0.37000 10.26200
[89] 5.38240 -15.50855 -0.65105 6.84655 -9.38970 1.70000 5.43200 10.17880 2.98075 -10.85725 -6.22135
[100] -1.89010 -3.46000 -2.29000 4.69180 1.20210 -0.95415 -3.40435 -7.49000 -0.15000 7.49445 -0.14045
[111] 3.35195 -2.63695 0.83655 -12.01000 11.16400 9.18425 -3.91240 6.02935 -2.27235 -4.32110 -3.16000
[122] 1.17000 0.74640 -0.12670 2.26105 0.04115 -2.31855 -2.30000 1.81400 0.68420 -0.94390 0.67445
[133] 7.34260 -8.99235 -3.63000 4.55800 5.04180 -5.30290 0.96565 2.37130 0.33100 -2.53000 6.50400
[144] 2.81535 -0.90240 -2.62860 -3.69285 0.61605 -5.81000 5.32800 -0.39985 0.52635 0.39335 0.56145
[155] -0.92660 -1.53000 10.51400 -3.33635 -4.52020 -1.85500 0.36355 -11.26815 1.62000 10.89000 -0.51330
[166] -9.56000 -1.79115 -14.12515 14.49105 -6.90000 2.76400 19.79725 -1.36595 -5.14430 1.67195 -9.18290
[177] -3.72000 18.14200 -2.22105 -5.47200 -1.74350 7.38740 -2.16620 -12.96000 12.10000 1.05790 4.37990
[188] 1.46360 -1.55485 -5.30650 -3.08000 9.59000 -0.70365 -0.29480 0.08570 -3.14220 -4.90170 -4.05690
[199] 0.82470 -0.61950 0.58260 -8.70000 16.49600 -2.05375 -1.15520 0.74145 0.49035 -9.79180 -1.06000
[210] 9.70800 -2.02660 -0.83785 1.14500 -1.46245 -4.42830 -16.34000 18.55000 1.93415 -3.23050 -3.63245
[221] 7.33200 -5.96870 -0.93000 2.03600 5.73760 -2.35745 5.82470 -2.38335 -5.71340 -7.60000 15.42000
[232] -1.36575 -1.38560 -5.07790 5.94480 -10.58400 -4.41000 11.21400 0.33840 1.42130 -3.75035 7.43265
[243] 2.78635 -15.19000 -1.36800 16.65735 -3.12405 -2.51290 3.51920 -9.63090 -0.95000 21.88000 -6.28715
[254] 6.66370 0.89995 -3.76125 -10.85840 -3.51000 13.74800 -3.02445 0.29100 0.33760 0.13705 -7.97660
[265] -1.15000 6.79400 -3.98745 -1.46300 2.38375 1.73950 -10.64685 -7.78000 15.32400 2.30850 -2.49665
[276] -2.86120 -1.30305 -2.09495 -2.23000 11.44800 -0.74900 0.83135 -3.44695 0.31755 -4.00500 -8.25000
[287] 16.45800 3.01845 1.72375 -3.42175 -2.29725 -4.55095 -8.92000 11.56400 -0.85435 -0.98450 1.37875
[298] -0.05225 -1.79065 -3.24000 6.89800 -2.88615 1.89680 -1.71995 1.23670 4.76065 1.91705 4.91885
[309] -3.09340 -9.62280 -6.96000 10.17000 1.00150 -0.27075 3.60460 3.91925 -12.29470 -0.15000 11.58200
[320] 1.95930 -9.38075 -1.46460 -5.29375 -1.29280 -2.95000 10.18800 -1.85795 -2.52690 3.46760 0.71515
[331] -3.87005 -4.41000 14.17200 5.00880 -1.75035 -7.26945 -4.97815 0.33920 -4.99000 -0.38000 13.42315
[342] 0.44745 -12.82670 5.65645 -1.60595 -4.19000 10.42800 -0.91455 2.01905 -1.22470 -6.72005 -2.37540
[353] -6.62000 11.03000 -0.16420 4.67650 2.10160 -5.40605 -5.63525 -7.84000 13.38400 -4.76505 -2.09520
[364] -0.21815 -5.53800 0.89805 0.84000 -3.79800 15.20915 -5.04190 2.85030 -3.54720 5.66655 -2.09000
[375] -3.51800 -2.05115 4.75600 1.64015 -2.93645 -2.78400 -4.60000 7.83000 7.58955 -4.06370 -3.14205
[386] -1.00330 -0.78590 -3.09000 16.99000 -1.75450 -0.17840 -6.17105 3.67985 -7.73185 -1.45000 -0.48000
[397] -0.03625 0.64765 -0.88770 4.37420 -3.41025 -4.16000 2.30600 3.64675 0.65160 -1.86050 4.44755
[408] -4.64435 0.59000 7.70800 0.13515 -3.63075 4.07175 -3.44855 1.61700 -2.69000 6.99000 -6.32835
[419] 0.06035 0.66905 2.09225 -8.90405 2.90000 -5.14800 1.27180 11.69970 18.22670 -5.02240 -11.80655
[430] -11.48000 22.61800 -2.89710 -21.47555 4.60950 0.11945 -1.44270 -1.49000 2.91800 5.56680 -4.15875
[441] -0.84530 -4.80830 2.38945 0.68000 6.40800 1.46045 6.47015 -3.83785 3.01765 -13.76620 3.80000
[452] 7.04600 -9.70915 5.06240 -4.42255 -4.30895 -0.48565 1.48000 1.01400 0.41325 4.26235 -0.52185
[463] -2.46555 0.05000 -1.16000 -0.26780 2.31460 7.02870 0.74975 -6.08455 -2.63000 2.65200 -5.02335
[474] 2.71820 14.30270 -14.36495 -2.38640 -4.52000 8.25600 -3.06855 -0.76365 4.48095 5.21815 -4.52365
[485] -1.52000 12.16000 -1.96610 8.04025 10.50445 -17.61300 -13.53880 -6.99000 7.25800 9.58530 3.87375
[496] -2.82975 2.68740 -11.12640 -3.71000 -2.09800 11.13375 -4.50495 -4.49020 3.06840 -3.24390 2.42000
[507] 10.54600 -6.76650 -1.39590 2.19665 -2.40210 -2.70515 -4.53000 6.81400 -2.62700 1.78745 -2.80275
[518] 5.00345 -5.79180 -0.41000 3.71200 1.10155 -0.99680 -1.49750 2.46750 -3.66735 -1.33000 1.89400
[529] 2.14050 -0.45820 -2.64025 1.64115 -2.63910 2.86000 -0.43000 -0.02565 -0.69105 0.18560 -0.08540
[540] -2.21505 -2.10000 7.17400 4.28040 -3.66650 -1.47560 1.35750 -2.93310 -2.36000 -0.04400 3.63380
[551] 1.57210 0.75930 -1.84085 -1.37640 -3.31000 4.11800 0.87930 3.56600 -0.25750 -2.47415 -2.73090
[562] -3.30000 5.78600 -3.72375 -0.40445 2.40250 1.75210 -3.30980 -2.22000 4.11600 -1.04385 1.09985
[573] 0.12310 -0.10000 0.85585 -3.19015 2.50105 2.59145 -9.87755 4.65000 4.64000 -0.38080 -2.83590
[584] 2.52765 -0.46655 -1.21260 -1.33000 7.19000 5.16895 -11.32840 5.50915 -0.29850 -4.34405 -1.16000
[595] 1.99200 0.94850 -0.11665 -1.32835 3.87565 -4.49720 -1.83000 10.71400 -3.08910 -2.57270 0.14165
[606] 0.67460 -3.13245 -10.58000 11.04000 4.67700 -0.76965 -8.59540 5.31355 -0.48550 -9.83000 13.65000
[617] 1.98135 -1.52210 -2.02930 -1.18050 -6.26230 4.41000 -1.84800 12.20420 -0.73490 -5.45060 2.30475
[628] -5.93990 -0.98000 8.14800 0.68375 -1.57665 -1.89040 1.30625 -6.66715 -1.09000 5.37200 6.03350
[639] -2.11330 -3.12840 7.26250 -9.10530 -7.88000 6.99800 1.24900 -1.71020 -0.42185 1.02915 -2.83645
[650] -2.63000 5.59000 0.04545 2.03685 5.21010 -2.90810 -7.58070 -0.74000 4.79800 -0.78570 6.57080
[661] -1.00055 0.71075 -8.20515 -2.34000 8.04000 5.03540 3.79530 1.00215 -4.29350 -8.57975 -3.60000
[672] 10.74600 0.03990 -1.48135 1.15855 -2.66635 -6.04255 -1.27000 6.89800 1.07795 -1.42805 -4.52400
[683] 3.48210 -10.79030 1.01000 10.82400 -2.22530 -0.37040 -3.96645 3.98060 -8.26920 4.43000 2.02000
[694] 5.57785 1.46695 -5.79030 1.21475 -4.58515 -0.81000 13.70000 1.10770 4.14880 -6.56570 -3.85860
[705] -5.86435 -2.84000 8.22800 0.06675 -0.60955 -2.53285 1.06340 2.27175 1.30000 7.91000 -11.03340
[716] -4.43140 3.40160 0.42545 -4.02465 -4.39000 14.80000 -11.49595 3.76795 9.23025 -3.32815 -7.07725
[727] -2.19000 11.81600 0.91175 -4.06940 -3.73460 -1.05750 -6.47590 -0.52000 7.66400 6.21025 -2.67520
[738] 1.26305 -1.17585 -6.75250 -4.32000 11.27200 -5.91505 1.08715 -0.00055 -3.66290 3.58325 -6.39000
[749] 13.22600 0.88190
I want to compare The distribution of this data with the one of a normal, so, I have the following code to try to do it, it first creates a histogram of the vector, then figure out the proportion between the frecuency and density, and finally I overlap a normal to the histogram using the lines function and the multiplier I just figured out:
histogram <- hist(Datadiff, breaks = 40)
multiplier2 <- unique(histogram$counts/histogram$density)[1]
lines(x = Datadiff, dnorm(Datadiff, sd = sd(Datadiff))*multiplier2, lwd = 3, lty = 2)
When the comparison is plotted, I get messy lines instead of the normal distribution:
Could please anyone point what is the problem?
If you want to keep frequency on the Y-axis instead of density, just use a sequence within the observed range in dnorm.
Your old code
histogram <- hist(Datadiff, breaks = 40)
multiplier2 <- unique(histogram$counts/histogram$density)[1]
New modification: make a sequence spanning the range of Datadiff
seq1 <- seq(min(Datadiff),max(Datadiff),.1)
Plot this sequence over the histogram
lines(x = seq1, dnorm(seq1,sd = sd(Datadiff))*multiplier2, lwd = 3, lty = 2)
You could also replace the mean of dnorm with the observed mean mean = mean(Datadiff)
You could use curve. Set freq=FALSE in histogram to get agreeing densities in both plots.
hist(Datadiff, breaks=40, freq=FALSE)
curve(dnorm(x, mean=mean(Datadiff), sd=sd(Datadiff)),
add=TRUE, col=2, lty=2)
legend("topright", c("empirical", "theoretical normal"), cex=.8, lty=1:2, col=1:2)
(If you want to compare with a standard normal don't use mean=mean(Datadiff), sd=sd(Datadiff), let your dnorm(.) default to mean=0, sd=1.)
Produces
Data
Datadiff <- c(-1.36785, -0.8615, 11.15045, -6.67, -20.52355, 8.738, -10.7468,
5.3824, -1.8901, 3.35195, 1.17, 7.3426, 2.81535, -0.9266, -9.56,
-3.72, 1.4636, 0.8247, 9.708, 7.332, -1.36575, 2.78635, 6.6637,
-1.15, -2.8612, 16.458, -0.05225, -3.0934, 1.9593, -3.87005,
0.44745, -6.62, -0.21815, -3.518, -1.0033, -0.03625, -4.64435,
0.06035, -11.48, -0.8453, 7.046, -2.46555, 2.7182, -1.52, -2.82975,
10.546, 5.00345, 2.1405, -2.21505, 1.5721, -3.3, 0.1231, 2.52765,
1.992, 0.6746, 1.98135, -5.9399, -2.1133, -2.63, -1.00055, 10.746,
3.4821, 5.57785, -5.86435, -4.4314, -2.19, 1.26305, 13.226, -0.01415,
-1.97, -2.1243, -8.4482, 6.576, 0.6822, 5.1788, -13.1987, -15.50855,
-3.46, -2.63695, 0.7464, -8.99235, -0.9024, -1.53, -1.79115,
18.142, -1.55485, -0.6195, -2.0266, -5.9687, -1.3856, -15.19,
0.89995, 6.794, -1.30305, 3.01845, -1.79065, -9.6228, -9.38075,
-4.41, -12.8267, 11.03, -5.538, -2.05115, -0.7859, 0.64765, 0.59,
0.66905, 22.618, -4.8083, -9.70915, 0.05, 14.3027, 12.16, 2.6874,
-6.7665, -5.7918, -0.4582, -2.1, 0.7593, 5.786, -0.1, -0.46655,
0.9485, -3.13245, -1.5221, -0.98, -3.1284, 5.59, 0.71075, 0.0399,
-10.7903, 1.46695, -2.84, 3.4016, 11.816, -1.17585, 0.8819, 2.6835,
6.622, 2.20255, -9.4959, -4.48095, 3.64315, -8.20225, 4.32, -0.65105,
-2.29, 0.83655, -0.1267, -3.63, -2.6286, 10.514, -14.12515, -2.22105,
-5.3065, 0.5826, -0.83785, -0.93, -5.0779, -1.368, -3.76125,
-3.98745, -2.09495, 1.72375, -3.24, -6.96, -1.4646, 14.172, 5.65645,
-0.1642, 0.89805, 4.756, -3.09, -0.8877, 7.708, 2.09225, -2.8971,
2.38945, 5.0624, -1.16, -14.36495, -1.9661, -11.1264, -1.3959,
-0.41, -2.64025, 7.174, -1.84085, -3.72375, 0.85585, -1.2126,
-0.11665, -10.58, -2.0293, 8.148, 7.2625, 0.04545, -8.20515,
-1.48135, 1.01, -5.7903, 8.228, 0.42545, 0.91175, -6.7525, -4.2698,
-0.8247, 2.0385, 0.13885, 0.05825, -0.46, -1.6177, -3.456, 6.84655,
4.6918, -12.01, 2.26105, 4.558, -3.69285, -3.33635, 14.49105,
-5.472, -3.08, -8.7, 1.145, 2.036, 5.9448, 16.65735, -10.8584,
-1.463, -2.23, -3.42175, 6.898, 10.17, -5.29375, 5.0088, -1.60595,
4.6765, 0.84, 1.64015, 16.99, 4.3742, 0.13515, -8.90405, -21.47555,
0.68, -4.42255, -0.2678, -2.3864, 8.04025, -3.71, 2.19665, 3.712,
1.64115, 4.2804, -1.3764, -0.40445, -3.19015, -1.33, -1.32835,
11.04, -1.1805, 0.68375, -9.1053, 2.03685, -2.34, 1.15855, 10.824,
1.21475, 0.06675, -4.02465, -4.0694, -4.32, -2.28975, -1.9953,
-0.19295, -1.06, 3.35215, 4.088, 23.66225, 0.2967, -9.3897, 1.2021,
11.164, 0.04115, 5.0418, 0.61605, -4.5202, -6.9, -1.7435, 9.59,
16.496, -1.46245, 5.7376, -10.584, -3.12405, -3.51, 2.38375,
11.448, -2.29725, -2.88615, 1.0015, -1.2928, -1.75035, -4.19,
2.1016, -3.798, -2.93645, -1.7545, -3.41025, -3.63075, 2.9, 4.6095,
6.408, -4.30895, 2.3146, -4.52, 10.50445, -2.098, -2.4021, 1.10155,
-2.6391, -3.6665, -3.31, 2.4025, 2.50105, 7.19, 3.87565, 4.677,
-6.2623, -1.57665, -7.88, 5.2101, 8.04, -2.66635, -2.2253, -4.58515,
-0.60955, -4.39, -3.7346, 11.272, -1.27, 1.84275, 1.83205, 3.858,
5.94525, 5.76705, -16.2386, 2.22515, 1.7, -0.95415, 9.18425,
-2.31855, -5.3029, -5.81, -1.855, 2.764, 7.3874, -0.70365, -2.05375,
-4.4283, -2.35745, -4.41, -2.5129, 13.748, 1.7395, -0.749, -4.55095,
1.8968, -0.27075, -2.95, -7.26945, 10.428, -5.40605, 15.20915,
-2.784, -0.1784, -4.16, 4.07175, -5.148, 0.11945, 1.46045, -0.48565,
7.0287, 8.256, -17.613, 11.13375, -2.70515, -0.9968, 2.86, -1.4756,
4.118, 1.7521, 2.59145, 5.16895, -4.4972, -0.76965, 4.41, -1.8904,
6.998, -2.9081, 5.0354, -6.04255, -0.3704, -0.81, -2.53285, 14.8,
-1.0575, -5.91505, 6.634, -4.70575, 2.043, -1.27255, -8.91095,
3.88775, -2.25, -1.77935, 5.432, -3.40435, -3.9124, -2.3, 0.96565,
5.328, 0.36355, 19.79725, -2.1662, -0.2948, -1.1552, -16.34,
5.8247, 11.214, 3.5192, -3.02445, -10.64685, 0.83135, -8.92,
-1.71995, 3.6046, 10.188, -4.97815, -0.91455, -5.63525, -5.0419,
-4.6, -6.17105, 2.306, -3.44855, 1.2718, -1.4427, 6.47015, 1.48,
0.74975, -3.06855, -13.5388, -4.50495, -4.53, -1.4975, -0.43,
1.3575, 0.8793, -3.3098, -9.87755, -11.3284, -1.83, -8.5954,
-1.848, 1.30625, 1.249, -7.5807, 3.7953, -1.27, -3.96645, 13.7,
1.0634, -11.49595, -6.4759, 1.08715, -3.13965, 3.6715, -3.51635,
0.3501, -1.21, -12.3292, -5.244, -0.26525, 10.1788, -7.49, 6.02935,
1.814, 2.3713, -0.39985, -11.26815, -1.36595, -12.96, 0.0857,
0.74145, 18.55, -2.38335, 0.3384, -9.6309, 0.291, -7.78, -3.44695,
11.564, 1.2367, 3.91925, -1.85795, 0.3392, 2.01905, -7.84, 2.8503,
7.83, 3.67985, 3.64675, 1.617, 11.6997, -1.49, -3.83785, 1.014,
-6.08455, -0.76365, -6.99, -4.4902, 6.814, 2.4675, -0.02565,
-2.9331, 3.566, -2.22, 4.65, 5.50915, 10.714, 5.31355, 12.2042,
-6.66715, -1.7102, -0.74, 1.00215, 6.898, 3.9806, 1.1077, 2.27175,
3.76795, -0.52, -0.00055, -1.6705, -1.009, -3.32, 3.38085, 7.998,
-0.08525, 8.21195, 0.3263, 2.98075, -0.15, -2.27235, 0.6842,
0.331, 0.52635, 1.62, -5.1443, 12.1, -3.1422, 0.49035, 1.93415,
-5.7134, 1.4213, -0.95, 0.3376, 15.324, 0.31755, -0.85435, 4.76065,
-12.2947, -2.5269, -4.99, -1.2247, 13.384, -3.5472, 7.58955,
-7.73185, 0.6516, -2.69, 18.2267, 2.918, 3.01765, 0.41325, -2.63,
4.48095, 7.258, 3.0684, -2.627, -3.66735, -0.69105, -2.36, -0.2575,
4.116, 4.64, -0.2985, -3.0891, -0.4855, -0.7349, -1.09, -0.42185,
4.798, -4.2935, 1.07795, -8.2692, 4.1488, 1.3, 9.23025, 7.664,
-3.6629, -0.2892, -1.00205, 9.078, -5.7087, -2.2173, -4.10745,
19.021, 0.37, -10.85725, 7.49445, -4.3211, -0.9439, -2.53, 0.39335,
10.89, 1.67195, 1.0579, -4.9017, -9.7918, -3.2305, -7.6, -3.75035,
21.88, 0.13705, 2.3085, -4.005, -0.9845, 1.91705, -0.15, 3.4676,
-0.38, -6.72005, -4.76505, 5.66655, -4.0637, -1.45, -1.8605,
6.99, -5.0224, 5.5668, -13.7662, 4.26235, 2.652, 5.21815, 9.5853,
-3.2439, 1.78745, -1.33, 0.1856, -0.044, -2.47415, -1.04385,
-0.3808, -4.34405, -2.5727, -9.83, -5.4506, 5.372, 1.02915, -0.7857,
-8.57975, -1.42805, 4.43, -6.5657, 7.91, -3.32815, 6.21025, 3.58325,
-0.42435, -0.7735, -0.6028, 5.08605, 13.6661, -6.7, -1.5533,
10.262, -6.22135, -0.14045, -3.16, 0.67445, 6.504, 0.56145, -0.5133,
-9.1829, 4.3799, -4.0569, -1.06, -3.63245, 15.42, 7.43265, -6.28715,
-7.9766, -2.49665, -8.25, 1.37875, 4.91885, 11.582, 0.71515,
13.42315, -2.3754, -2.0952, -2.09, -3.14205, -0.48, 4.44755,
-6.32835, -11.80655, -4.15875, 3.8, -0.52185, -5.02335, -4.52365,
3.87375, 2.42, -2.80275, 1.894, -0.0854, 3.6338, -2.7309, 1.09985,
-2.8359, -1.16, 0.14165, 13.65, 2.30475, 6.0335, -2.83645, 6.5708,
-3.6, -4.524, 2.02, -3.8586, -11.0334, -7.07725, -2.6752, -6.39
)

plotting a distribution in R using percentiles and corresponding values

I would like to plot the SAT dataset found here
https://blog.prepscholar.com/sat-percentiles-high-precision-2016
My issue with doing this is that I am unsure if there is a more elegant way to do this than I am doing. Currently, I am converting those percentiles to Z scores and then using that associated probability to create a distribution. The problem that I have is
The SAT scores do not seem to be normally distributed. They approximate a normal distribution, but they do not seem to be normally distributed. This makes using the tried and true rnorm (X, mean = 1000, sd = 200) unusable.
I am not sure if there is a better way than converting the percentiles to Z scores since the SAT scores do not seem to be normally distributed.
Any help would be appreciated.
My long term plan is to create a shiny ap where a person can use a slider to move along the distribution to see where their score puts them in what percentile. But the first step is to actually figure out a way to produce an accurate distribution.
Here is the code in total:
scores <- as.numeric(
c(
1600, 1593, 1587, 1580, 1573, 1567,
1560, 1553, 1547, 1540, 1533, 1527,
1520, 1513, 1507, 1500, 1493, 1487,
1480, 1473, 1467, 1460, 1453, 1447,
1440, 1433, 1427, 1420, 1413, 1407,
1400, 1393, 1387, 1380, 1373, 1367,
1360, 1353, 1347, 1340, 1333, 1327,
1320, 1313, 1307, 1300, 1293, 1287,
1280, 1273, 1267, 1260, 1253, 1247,
1240, 1233, 1227, 1220, 1213, 1207,
1200, 1193, 1187, 1180, 1173, 1167,
1160, 1153, 1147, 1140, 1133, 1127,
1120, 1113, 1107, 1100, 1093, 1087,
1080, 1073, 1067, 1060, 1053, 1047,
1040, 1033, 1027, 1020, 1013, 1007,
1000, 0993, 0987, 0980, 0973, 0967,
0960, 0953, 0947, 0940, 0933, 0927,
0920, 0913, 0907, 0900, 0893, 0887,
0880, 0873, 0867, 0860, 0853, 0847,
0840, 0833, 0827, 0820, 0813, 0807,
0800, 0793, 0787, 0780, 0773, 0767,
0760, 0753, 0747, 0740, 0733, 0727,
0720, 0713, 0707, 0700, 0693, 0687,
0680, 0673, 0667, 0660, 0653, 0647,
0640, 0633, 0627, 0620, 0613, 0607,
0600, 0593, 0587, 0580, 0573, 0567,
0560, 0553, 0547, 0540, 0533, 0527,
0520, 0513, 0507, 0500, 0493, 0487,
0480, 0473, 0467, 0460, 0453, 0447,
0440, 0433, 0427, 0420, 0413, 0407,
0400
)
)
probs <- as.numeric(
c(
0.000665335, 0.001508711, 0.002067932, 0.002878073, 0.003976493, 0.005137125,
0.006483906, 0.008169371, 0.010066473, 0.012051019, 0.014095858,
0.016307247, 0.01851226, 0.020808986, 0.023364025, 0.02601564,
0.028721866, 0.031560739, 0.034551519, 0.037733663, 0.041036965,
0.044427406, 0.047928757, 0.051538726, 0.055292787, 0.059145645,
0.063187376, 0.067367682, 0.071691308, 0.076255174, 0.080943422,
0.085690142, 0.090464868, 0.095379534, 0.100511283, 0.105758358,
0.111083521, 0.116571178, 0.122285078, 0.128123463, 0.13397113,
0.139830118, 0.145699225, 0.151666124, 0.157793404, 0.164023115,
0.170358704, 0.17676173, 0.183278673, 0.189955902, 0.196651135,
0.203363202, 0.210071134, 0.216801722, 0.223562248, 0.230347853,
0.237183732, 0.243934459, 0.250657665, 0.257436698, 0.264227682,
0.270968969, 0.277599004, 0.284165141, 0.290648753, 0.297040829,
0.303401865, 0.30964291, 0.315714288, 0.321696268, 0.3275473,
0.33324613, 0.338771004, 0.344104024, 0.34924206, 0.354185931,
0.358956451, 0.363504804, 0.36782396, 0.371962147, 0.375851065,
0.37943538, 0.382754048, 0.38580143, 0.388568593, 0.391025026,
0.393142417, 0.394947023, 0.39643261, 0.397582763, 0.398388967,
0.398835641, 0.398933788, 0.398676885, 0.398065535, 0.397089484,
0.395745285, 0.394041326, 0.391951596, 0.389478503, 0.386655445,
0.383491062, 0.379943329, 0.376014793, 0.371758542, 0.367167906,
0.362242796, 0.356970877, 0.351398549, 0.345499578, 0.33929925,
0.332851353, 0.326122779, 0.31911459, 0.311858166, 0.304408822,
0.296767961, 0.288895933, 0.280893084, 0.272865458, 0.264642129,
0.256259902, 0.247931807, 0.239607, 0.231228792, 0.222875786,
0.214544714, 0.206273084, 0.198003801, 0.189731661, 0.181671447,
0.173826196, 0.16610093, 0.158499433, 0.151067478, 0.143751108,
0.136558209, 0.129683746, 0.123040703, 0.11660098, 0.110455988,
0.104559737, 0.098857524, 0.093254931, 0.087923914, 0.08286171,
0.077907056, 0.073124914, 0.068583012, 0.064242767, 0.059980362,
0.055906234, 0.052062062, 0.048298984, 0.044678669, 0.041258641,
0.03796372, 0.034841037, 0.03185105, 0.028992313, 0.026330747,
0.023870486, 0.0215867, 0.019418127, 0.017379035, 0.015436224,
0.01358768, 0.011905384, 0.01032104, 0.008865584, 0.007564083,
0.006380472, 0.005299104, 0.004317991, 0.003512008, 0.00284036,
0.002268168, 0.001724469, 0.001327348, 0.001030367, 0.000112
)
)
data <- as.data.frame (cbind (scores, probs))
data2 <- sample (data$scores, 10000000, replace = T, prob = data$probs)
den <- density (data2, adjust = 2)
plot (den , xlim = c(400,1600))

How to specify explicitly the month section while plotting a seasonal plot?

I am trying to learn forecast using fpp2 package on bitcoin data. The bitcoin_ts excel just has two columns, one the date column another the closing price for each date.
library(fpp2)
bitcoin_ts <- read_excel("bitcoin_ts.xlsx")
head(bitcoin_ts)
myts <- ts(bitcoin_ts[,2], start = c(2013, 2), frequency = 366)
##autoplot(myts, facets = FALSE)
ggseasonplot(myts)
As beginning of the data is 2013 and from the month of April, which is the second quarter, I specified start as 2013 and 2 and frequency I gave as number of days in a year.
I get a graph as attached the lower scale (season), my requirement is to show months but I guess due to the all the dates being included in the data it is getting superimposed. How can I fix this to achieve only months over there?
I would like to see something similar to this chart (of course the data upon which this chart was plotted had only year and month (ex: Mar-81)
Unable to attach the entire 1655 observations as the this textbox is set to a limit of 30K characters.
However I attaching the link to the dataset as well.
Complete Dataset
Below find the sample of first 500,
> dput(head(bitcoin_ts,500))
structure(list(Date = structure(c(1367107200, 1367193600, 1367280000,
1367366400, 1367452800, 1367539200, 1367625600, 1367712000, 1367798400,
1367884800, 1367971200, 1368057600, 1368144000, 1368230400, 1368316800,
1368403200, 1368489600, 1368576000, 1368662400, 1368748800, 1368835200,
1368921600, 1369008000, 1369094400, 1369180800, 1369267200, 1369353600,
1369440000, 1369526400, 1369612800, 1369699200, 1369785600, 1369872000,
1369958400, 1370044800, 1370131200, 1370217600, 1370304000, 1370390400,
1370476800, 1370563200, 1370649600, 1370736000, 1370822400, 1370908800,
1370995200, 1371081600, 1371168000, 1371254400, 1371340800, 1371427200,
1371513600, 1371600000, 1371686400, 1371772800, 1371859200, 1371945600,
1372032000, 1372118400, 1372204800, 1372291200, 1372377600, 1372464000,
1372550400, 1372636800, 1372723200, 1372809600, 1372896000, 1372982400,
1373068800, 1373155200, 1373241600, 1373328000, 1373414400, 1373500800,
1373587200, 1373673600, 1373760000, 1373846400, 1373932800, 1374019200,
1374105600, 1374192000, 1374278400, 1374364800, 1374451200, 1374537600,
1374624000, 1374710400, 1374796800, 1374883200, 1374969600, 1375056000,
1375142400, 1375228800, 1375315200, 1375401600, 1375488000, 1375574400,
1375660800, 1375747200, 1375833600, 1375920000, 1376006400, 1376092800,
1376179200, 1376265600, 1376352000, 1376438400, 1376524800, 1376611200,
1376697600, 1376784000, 1376870400, 1376956800, 1377043200, 1377129600,
1377216000, 1377302400, 1377388800, 1377475200, 1377561600, 1377648000,
1377734400, 1377820800, 1377907200, 1377993600, 1378080000, 1378166400,
1378252800, 1378339200, 1378425600, 1378512000, 1378598400, 1378684800,
1378771200, 1378857600, 1378944000, 1379030400, 1379116800, 1379203200,
1379289600, 1379376000, 1379462400, 1379548800, 1379635200, 1379721600,
1379808000, 1379894400, 1379980800, 1380067200, 1380153600, 1380240000,
1380326400, 1380412800, 1380499200, 1380585600, 1380672000, 1380758400,
1380844800, 1380931200, 1381017600, 1381104000, 1381190400, 1381276800,
1381363200, 1381449600, 1381536000, 1381622400, 1381708800, 1381795200,
1381881600, 1381968000, 1382054400, 1382140800, 1382227200, 1382313600,
1382400000, 1382486400, 1382572800, 1382659200, 1382745600, 1382832000,
1382918400, 1383004800, 1383091200, 1383177600, 1383264000, 1383350400,
1383436800, 1383523200, 1383609600, 1383696000, 1383782400, 1383868800,
1383955200, 1384041600, 1384128000, 1384214400, 1384300800, 1384387200,
1384473600, 1384560000, 1384646400, 1384732800, 1384819200, 1384905600,
1384992000, 1385078400, 1385164800, 1385251200, 1385337600, 1385424000,
1385510400, 1385596800, 1385683200, 1385769600, 1385856000, 1385942400,
1386028800, 1386115200, 1386201600, 1386288000, 1386374400, 1386460800,
1386547200, 1386633600, 1386720000, 1386806400, 1386892800, 1386979200,
1387065600, 1387152000, 1387238400, 1387324800, 1387411200, 1387497600,
1387584000, 1387670400, 1387756800, 1387843200, 1387929600, 1388016000,
1388102400, 1388188800, 1388275200, 1388361600, 1388448000, 1388534400,
1388620800, 1388707200, 1388793600, 1388880000, 1388966400, 1389052800,
1389139200, 1389225600, 1389312000, 1389398400, 1389484800, 1389571200,
1389657600, 1389744000, 1389830400, 1389916800, 1390003200, 1390089600,
1390176000, 1390262400, 1390348800, 1390435200, 1390521600, 1390608000,
1390694400, 1390780800, 1390867200, 1390953600, 1391040000, 1391126400,
1391212800, 1391299200, 1391385600, 1391472000, 1391558400, 1391644800,
1391731200, 1391817600, 1391904000, 1391990400, 1392076800, 1392163200,
1392249600, 1392336000, 1392422400, 1392508800, 1392595200, 1392681600,
1392768000, 1392854400, 1392940800, 1393027200, 1393113600, 1393200000,
1393286400, 1393372800, 1393459200, 1393545600, 1393632000, 1393718400,
1393804800, 1393891200, 1393977600, 1394064000, 1394150400, 1394236800,
1394323200, 1394409600, 1394496000, 1394582400, 1394668800, 1394755200,
1394841600, 1394928000, 1395014400, 1395100800, 1395187200, 1395273600,
1395360000, 1395446400, 1395532800, 1395619200, 1395705600, 1395792000,
1395878400, 1395964800, 1396051200, 1396137600, 1396224000, 1396310400,
1396396800, 1396483200, 1396569600, 1396656000, 1396742400, 1396828800,
1396915200, 1397001600, 1397088000, 1397174400, 1397260800, 1397347200,
1397433600, 1397520000, 1397606400, 1397692800, 1397779200, 1397865600,
1397952000, 1398038400, 1398124800, 1398211200, 1398297600, 1398384000,
1398470400, 1398556800, 1398643200, 1398729600, 1398816000, 1398902400,
1398988800, 1399075200, 1399161600, 1399248000, 1399334400, 1399420800,
1399507200, 1399593600, 1399680000, 1399766400, 1399852800, 1399939200,
1400025600, 1400112000, 1400198400, 1400284800, 1400371200, 1400457600,
1400544000, 1400630400, 1400716800, 1400803200, 1400889600, 1400976000,
1401062400, 1401148800, 1401235200, 1401321600, 1401408000, 1401494400,
1401580800, 1401667200, 1401753600, 1401840000, 1401926400, 1402012800,
1402099200, 1402185600, 1402272000, 1402358400, 1402444800, 1402531200,
1402617600, 1402704000, 1402790400, 1402876800, 1402963200, 1403049600,
1403136000, 1403222400, 1403308800, 1403395200, 1403481600, 1403568000,
1403654400, 1403740800, 1403827200, 1403913600, 1.404e+09, 1404086400,
1404172800, 1404259200, 1404345600, 1404432000, 1404518400, 1404604800,
1404691200, 1404777600, 1404864000, 1404950400, 1405036800, 1405123200,
1405209600, 1405296000, 1405382400, 1405468800, 1405555200, 1405641600,
1405728000, 1405814400, 1405900800, 1405987200, 1406073600, 1406160000,
1406246400, 1406332800, 1406419200, 1406505600, 1406592000, 1406678400,
1406764800, 1406851200, 1406937600, 1407024000, 1407110400, 1407196800,
1407283200, 1407369600, 1407456000, 1407542400, 1407628800, 1407715200,
1407801600, 1407888000, 1407974400, 1408060800, 1408147200, 1408233600,
1408320000, 1408406400, 1408492800, 1408579200, 1408665600, 1408752000,
1408838400, 1408924800, 1409011200, 1409097600, 1409184000, 1409270400,
1409356800, 1409443200, 1409529600, 1409616000, 1409702400, 1409788800,
1409875200, 1409961600, 1410048000, 1410134400, 1410220800), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), Close = c(134.21, 144.54, 139, 116.99,
105.21, 97.75, 112.5, 115.91, 112.3, 111.5, 113.57, 112.67, 117.2,
115.24, 115, 117.98, 111.5, 114.22, 118.76, 123.02, 123.5, 121.99,
122, 122.88, 123.89, 126.7, 133.2, 131.98, 133.48, 129.75, 129,
132.3, 128.8, 129, 129.3, 122.29, 122.22, 121.42, 121.65, 118,
111.5, 108.3, 100, 106.35, 108.9, 108.15, 104, 99.98, 99.99,
99.51, 101.7, 107.4, 108.25, 110.15, 109.5, 108.3, 107.6, 102.74,
103.95, 104, 101.44, 94.65, 94.99, 96.61, 88.05, 90.13, 77.53,
80.53, 68.43, 70.28, 74.56, 76.52, 76.69, 86.76, 88.98, 93.59,
98.13, 94.69, 98.4, 97.45, 98.5, 90.58, 92.17, 89.39, 90.76,
91.61, 95.56, 94.51, 96.9, 96.02, 94.12, 99.76, 101.2, 107.99,
106.09, 104, 104.5, 104, 105.14, 106.22, 106.75, 106.75, 103,
102.8, 103, 105, 106.64, 109, 112.56, 109.99, 108.99, 113.5,
113.5, 119, 121.2, 123.3, 121.15, 118.5, 120.05, 122.11, 120.06,
126.5, 122.62, 122.39, 133.49, 135.35, 138.34, 135.85, 136.77,
126.74, 126.43, 119.15, 124.15, 121.66, 127.11, 125.91, 135.25,
133.13, 134.98, 129.22, 130.37, 131.72, 131.66, 131.47, 129.65,
127.04, 127.43, 129.12, 125.95, 127.25, 128.22, 128.38, 133.78,
134.78, 137.34, 133, 132.18, 114.13, 123.63, 129.01, 128.55,
129, 126.94, 126, 130.69, 130.59, 130.9, 135.19, 138.13, 140.52,
145.24, 142.55, 146.25, 155.96, 172.42, 174.61, 182.21, 193.76,
213.62, 198.23, 186.69, 177.32, 196.44, 198.55, 204.39, 199.97,
204, 206.18, 206.22, 215.05, 229.1, 245.24, 262.5, 296.41, 338.11,
339.11, 326.62, 342.44, 360.33, 407.37, 420.2, 417.95, 440.22,
492.11, 703.56, 584.61, 590.83, 722.43, 771.44, 797.82, 774.25,
799.11, 928.1, 1001.96, 1031.95, 1131.97, 1129.43, 955.85, 1043.33,
1078.28, 1151.17, 1045.11, 829.45, 698.23, 795.87, 893.19, 988.51,
878.48, 873.26, 892.58, 872.6, 876.12, 705.97, 682.12, 522.7,
691.96, 625.32, 605.66, 617.18, 673.41, 665.58, 682.21, 761.98,
735.07, 727.83, 745.05, 756.13, 754.01, 771.4, 802.39, 818.72,
859.51, 933.53, 953.29, 802, 842.72, 846.86, 868.48, 913.95,
863.22, 841.2, 833.27, 860.9, 835.63, 814.64, 840, 870.96, 870.2,
863.91, 845.59, 822.04, 797.07, 853.61, 885.28, 771.39, 812.51,
826, 819.03, 829.92, 832.58, 825.37, 823.83, 827.96, 811.91,
781.55, 712.4, 673.92, 682.9, 681.03, 672.17, 651.72, 605.24,
661.99, 650.92, 616.63, 626.27, 626.6, 623.03, 556.14, 574.16,
605.42, 605.82, 546.32, 538.71, 582.69, 578.77, 549.26, 565.61,
559.79, 667.76, 666.78, 665.51, 663.86, 629.15, 617.45, 636.96,
627.79, 634.11, 632.1, 638.14, 628.8, 636.12, 631.11, 622.37,
614.83, 609.89, 588.77, 571.49, 565.04, 561.27, 583.41, 583.92,
580.83, 471.24, 495.67, 491.17, 460.27, 457, 478.38, 437.14,
444.72, 447.53, 461.91, 460.5, 449.42, 453.09, 442.73, 365.18,
420.95, 421.12, 414.06, 458.79, 515.59, 527.39, 495.96, 479.64,
501.56, 498.17, 495.77, 487.92, 491.3, 500.46, 461.45, 458.6,
436.39, 440.29, 447.21, 447.64, 457.76, 449.38, 437.76, 436.4,
433.48, 428.96, 438.82, 440.17, 449.46, 454.43, 438.89, 441.46,
440.67, 443.97, 447.25, 448.06, 448.9, 446.26, 446.18, 485.72,
491.77, 524.58, 520.22, 525.14, 571.59, 583.42, 571.24, 577.06,
568.18, 615.33, 623.68, 630.23, 660.62, 667.61, 641.61, 659.26,
653.7, 654.97, 656.14, 649.16, 653.15, 633.02, 586.95, 600.16,
577.36, 592.94, 592.19, 610.86, 607.96, 598.07, 594.15, 594.99,
602.27, 593.98, 582.36, 566.34, 581.14, 597.26, 596.55, 602.72,
639.8, 640.81, 650.88, 645.16, 630.69, 631.46, 635.81, 624.09,
624.82, 624.51, 616.76, 632, 633.71, 626.5, 619.32, 621.59, 616.8,
623.09, 628.78, 628.51, 623.9, 622.21, 621.55, 619.41, 601.73,
601.09, 595.81, 593.85, 585.69, 584.73, 567.29, 586.24, 594.92,
589.33, 586.67, 588.78, 585.44, 584.65, 588.87, 592.58, 589.37,
591.06, 576.37, 569.64, 546.66, 505.97, 497.01, 519.71, 491.8,
461.46, 485.25, 511.98, 517.24, 514.04, 498.07, 508.29, 502.5,
511.57, 511.15, 507.81, 508.52, 504.25, 477.76, 474.88, 477.43,
477.59, 489.66, 483.34, 484.83, 482.28, 474.6, 475.26)), .Names = c("Date",
"Close"), row.names = c(NA, -500L), class = c("tbl_df", "tbl",
"data.frame"))

R forecast function not picking up seasonality

I am having trouble picking up the seasonality the seems to be implied in the data. I think (though its just a guess that its using additive and not multiplicative seasonality). I am using the forecast function and thought it would automatically pick what I need based on a lecture from Dr. Hyndman. The following snipet of code plots the chart and I would have expected the forecast to be higher then it is. Am I missing a model parameter or something? Any help would be appreciated.
sw<-c(2280, 1754, 1667, 1359, 1285, 1379, 2166, 1053, 1076, 1149, 1277, 1577, 1639, 1719, 1592, 2306, 3075, 2897, 1875, 1966, 2927, 3528, 2948, 2890, 3947, 3913, 3885, 4148, 5293, 5752, 6001, 7719, 5512, 6782, 6320, 6425, 6406, 7237, 8655, 9269, 12447, 13470, 13469, 13949, 17753, 17653, 14531, 14496, 13643, 12652, 12665, 10629, 8962, 8198, 6833, 5027, 4407, 4449, 4399, 5896, 6589, 3786, 4386, 4847, 5597, 5407, 4800, 7803, 9255, 10423, 5523, 8121, 6944, 8434, 9847, 9292, 9794, 10195, 10124, 11310, 12245, 12798, 14611, 15402, 13532, 16154, 15101, 14755, 17139, 16475, 19935, 19980, 25173, 28568, 27839, 28991, 27073, 29615, 25849, 27910, 27067, 21303, 20544, 15188, 13706, 9277, 10815, 7228, 4608, 4409, 9866, 8471, 8223, 6445, 6641, 6833, 11421, 8945, 8127, 10380, 12005, 13272, 9431, 12144, 14934, 14052, 11712, 14888, 15824, 17275, 18067, 19839, 21192, 22763, 22976, 23721, 22681, 20131, 19965, 20539, 19517, 22022, 23076, 30574, 40247, 43111, 39577, 40724, 44982, 44388, 46372, 43153, 36821, 32258, 31256, 27153, 23180, 18252, 16381, 13220, 12500, 10727, 9636, 8892, 8644, 9482, 9170, 10937, 12299, 15781, 11477, 16524, 16752, 18072, 14776, 13388, 18056, 19815, 21263, 22046, 26415, 24247, 25403, 30058, 26331, 32533, 31891, 35973, 27558, 24554, 25692, 25955, 24284, 24930, 28354, 34840, 40055, 42099, 42768, 48279, 50086, 56466, 42244, 51451, 44583, 39091, 33391, 29452, 25533)
swts <- ts(sw, frequency=52, start=c(2006,30))
swfc <- forecast(swts,h=52)
plot(swfc)
Did you data have multiple seasonal periods? If so you could check the tbats function.
Anyway, your seasonal period is greater than 12, so forecast is using a stl decomposition to adjust your seasonal data. Maybe you wanna check ?stlf for more info on what parameters you can change, or try a BoxCox transformation:
lambda <- BoxCox.lambda(sw)
swfc <- forecast(swts,h=52, lambda = lambda, robust = TRUE)
plot(swfc)

Resources