When I try and generate a plot with the below I receive an Object not found for the fertility rate.
I've used str and names to make sure I've got the name correct.
there is a file ("https://sds-platform-private.s3-us-east-2.amazonaws.com/uploads/P2-Section5-Homework-Data.csv) that I have to use also.
From what I can see everything should run ok. But I've obviously done something wrong as I get:
Error in plot(data = New_Data1960, x = Fertility.Rate, y =
Life_Expectancy_At_Birth_1960, : object 'Fertility.Rate' not found
Can someone explain why I'm getting this error and how I can fix it?
#Execute below code to generate three new vectors
Country_Code <- c("ABW","AFG","AGO","ALB","ARE","ARG","ARM","ATG","AUS","AUT","AZE","BDI","BEL","BEN","BFA","BGD","BGR","BHR","BHS","BIH","BLR","BLZ","BOL","BRA","BRB","BRN","BTN","BWA","CAF","CAN","CHE","CHL","CHN","CIV","CMR","COG","COL","COM","CPV","CRI","CUB","CYP","CZE","DEU","DJI","DNK","DOM","DZA","ECU","EGY","ERI","ESP","EST","ETH","FIN","FJI","FRA","FSM","GAB","GBR","GEO","GHA","GIN","GMB","GNB","GNQ","GRC","GRD","GTM","GUM","GUY","HKG","HND","HRV","HTI","HUN","IDN","IND","IRL","IRN","IRQ","ISL","ITA","JAM","JOR","JPN","KAZ","KEN","KGZ","KHM","KIR","KOR","KWT","LAO","LBN","LBR","LBY","LCA","LKA","LSO","LTU","LUX","LVA","MAC","MAR","MDA","MDG","MDV","MEX","MKD","MLI","MLT","MMR","MNE","MNG","MOZ","MRT","MUS","MWI","MYS","NAM","NCL","NER","NGA","NIC","NLD","NOR","NPL","NZL","OMN","PAK","PAN","PER","PHL","PNG","POL","PRI","PRT","PRY","PYF","QAT","ROU","RUS","RWA","SAU","SDN","SEN","SGP","SLB","SLE","SLV","SOM","SSD","STP","SUR","SVK","SVN","SWE","SWZ","SYR","TCD","TGO","THA","TJK","TKM","TLS","TON","TTO","TUN","TUR","TZA","UGA","UKR","URY","USA","UZB","VCT","VEN","VIR","VNM","VUT","WSM","YEM","ZAF","COD","ZMB","ZWE")
Life_Expectancy_At_Birth_1960 <- c(65.5693658536586,32.328512195122,32.9848292682927,62.2543658536585,52.2432195121951,65.2155365853659,65.8634634146342,61.7827317073171,70.8170731707317,68.5856097560976,60.836243902439,41.2360487804878,69.7019512195122,37.2782682926829,34.4779024390244,45.8293170731707,69.2475609756098,52.0893658536585,62.7290487804878,60.2762195121951,67.7080975609756,59.9613658536585,42.1183170731707,54.2054634146342,60.7380487804878,62.5003658536585,32.3593658536585,50.5477317073171,36.4826341463415,71.1331707317073,71.3134146341463,57.4582926829268,43.4658048780488,36.8724146341463,41.523756097561,48.5816341463415,56.716756097561,41.4424390243903,48.8564146341463,60.5761951219512,63.9046585365854,69.5939268292683,70.3487804878049,69.3129512195122,44.0212682926829,72.1765853658537,51.8452682926829,46.1351219512195,53.215,48.0137073170732,37.3629024390244,69.1092682926829,67.9059756097561,38.4057073170732,68.819756097561,55.9584878048781,69.8682926829268,57.5865853658537,39.5701219512195,71.1268292682927,63.4318536585366,45.8314634146342,34.8863902439024,32.0422195121951,37.8404390243902,36.7330487804878,68.1639024390244,59.8159268292683,45.5316341463415,61.2263414634146,60.2787317073171,66.9997073170732,46.2883170731707,64.6086585365854,42.1000975609756,68.0031707317073,48.6403170731707,41.1719512195122,69.691756097561,44.945512195122,48.0306829268293,73.4286585365854,69.1239024390244,64.1918292682927,52.6852682926829,67.6660975609756,58.3675853658537,46.3624146341463,56.1280731707317,41.2320243902439,49.2159756097561,53.0013170731707,60.3479512195122,43.2044634146342,63.2801219512195,34.7831707317073,42.6411951219512,57.303756097561,59.7471463414634,46.5107073170732,69.8473170731707,68.4463902439024,69.7868292682927,64.6609268292683,48.4466341463415,61.8127804878049,39.9746829268293,37.2686341463415,57.0656341463415,60.6228048780488,28.2116097560976,67.6017804878049,42.7363902439024,63.7056097560976,48.3688048780488,35.0037073170732,43.4830975609756,58.7452195121951,37.7736341463415,59.4753414634146,46.8803902439024,58.6390243902439,35.5150487804878,37.1829512195122,46.9988292682927,73.3926829268293,73.549756097561,35.1708292682927,71.2365853658537,42.6670731707317,45.2904634146342,60.8817073170732,47.6915853658537,57.8119268292683,38.462243902439,67.6804878048781,68.7196097560976,62.8089268292683,63.7937073170732,56.3570487804878,61.2060731707317,65.6424390243903,66.0552926829268,42.2492926829268,45.6662682926829,48.1876341463415,38.206,65.6598292682927,49.3817073170732,30.3315365853659,49.9479268292683,36.9658780487805,31.6767073170732,50.4513658536585,59.6801219512195,69.9759268292683,68.9780487804878,73.0056097560976,44.2337804878049,52.768243902439,38.0161219512195,40.2728292682927,54.6993170731707,56.1535365853659,54.4586829268293,33.7271219512195,61.3645365853659,62.6575853658537,42.009756097561,45.3844146341463,43.6538780487805,43.9835609756098,68.2995365853659,67.8963902439025,69.7707317073171,58.8855365853659,57.7238780487805,59.2851219512195,63.7302195121951,59.0670243902439,46.4874878048781,49.969512195122,34.3638048780488,49.0362926829268,41.0180487804878,45.1098048780488,51.5424634146342)
Life_Expectancy_At_Birth_2013 <- c(75.3286585365854,60.0282682926829,51.8661707317073,77.537243902439,77.1956341463415,75.9860975609756,74.5613658536585,75.7786585365854,82.1975609756098,80.890243902439,70.6931463414634,56.2516097560976,80.3853658536585,59.3120243902439,58.2406341463415,71.245243902439,74.4658536585366,76.5459512195122,75.0735365853659,76.2769268292683,72.4707317073171,69.9820487804878,67.9134390243903,74.1224390243903,75.3339512195122,78.5466585365854,69.1029268292683,64.3608048780488,49.8798780487805,81.4011219512195,82.7487804878049,81.1979268292683,75.3530243902439,51.2084634146342,55.0418048780488,61.6663902439024,73.8097317073171,62.9321707317073,72.9723658536585,79.2252195121951,79.2563902439025,79.9497804878049,78.2780487804878,81.0439024390244,61.6864634146342,80.3024390243903,73.3199024390244,74.5689512195122,75.648512195122,70.9257804878049,63.1778780487805,82.4268292682927,76.4243902439025,63.4421951219512,80.8317073170732,69.9179268292683,81.9682926829268,68.9733902439024,63.8435853658537,80.9560975609756,74.079512195122,61.1420731707317,58.216487804878,59.9992682926829,54.8384146341464,57.2908292682927,80.6341463414634,73.1935609756098,71.4863902439024,78.872512195122,66.3100243902439,83.8317073170732,72.9428536585366,77.1268292682927,62.4011463414634,75.2682926829268,68.7046097560976,67.6604146341463,81.0439024390244,75.1259756097561,69.4716829268293,83.1170731707317,82.290243902439,73.4689268292683,73.9014146341463,83.3319512195122,70.45,60.9537804878049,70.2024390243902,67.7720487804878,65.7665853658537,81.459756097561,74.462756097561,65.687243902439,80.1288780487805,60.5203902439024,71.6576829268293,74.9127073170732,74.2402926829268,49.3314634146342,74.1634146341464,81.7975609756098,73.9804878048781,80.3391463414634,73.7090487804878,68.811512195122,64.6739024390244,76.6026097560976,76.5326585365854,75.1870487804878,57.5351951219512,80.7463414634146,65.6540975609756,74.7583658536585,69.0618048780488,54.641512195122,62.8027073170732,74.46,61.466,74.567512195122,64.3438780487805,77.1219512195122,60.8281463414634,52.4421463414634,74.514756097561,81.1048780487805,81.4512195121951,69.222,81.4073170731707,76.8410487804878,65.9636829268293,77.4192195121951,74.2838536585366,68.1315609756097,62.4491707317073,76.8487804878049,78.7111951219512,80.3731707317073,72.7991707317073,76.3340731707317,78.4184878048781,74.4634146341463,71.0731707317073,63.3948292682927,74.1776341463415,63.1670487804878,65.878756097561,82.3463414634146,67.7189268292683,50.3631219512195,72.4981463414634,55.0230243902439,55.2209024390244,66.259512195122,70.99,76.2609756097561,80.2780487804878,81.7048780487805,48.9379268292683,74.7157804878049,51.1914878048781,59.1323658536585,74.2469268292683,69.4001707317073,65.4565609756098,67.5223658536585,72.6403414634147,70.3052926829268,73.6463414634147,75.1759512195122,64.2918292682927,57.7676829268293,71.159512195122,76.8361951219512,78.8414634146341,68.2275853658537,72.8108780487805,74.0744146341464,79.6243902439024,75.756487804878,71.669243902439,73.2503902439024,63.583512195122,56.7365853658537,58.2719268292683,59.2373658536585,55.633)
#(c) Kirill Eremenko, www.superdatascience.com
P2.Section5.Homework.Data <- read.csv("H:/Program Files/RStudio/P2-Section5-Homework-Data.csv")
View(P2.Section5.Homework.Data)
head(P2.Section5.Homework.Data)
#generate a data.frame from loaded values
yr1960 <- data.frame(Country_Code, Life_Expectancy_At_Birth_1960)
yr2013 <- data.frame(Country_Code, Life_Expectancy_At_Birth_2013)
#filter master data to only ncessary periods
P2_1960 <- P2.Section5.Homework.Data[P2.Section5.Homework.Data$Year == 1960,]
P2_2013 <- P2.Section5.Homework.Data[P2.Section5.Homework.Data$Year == 2013,]
head(P2_1960)
#merge data into one frame
New_Data1960 <- merge(P2_1960,yr1960, by.x = "Country.Code", by.y = "Country_Code")
head(New_Data1960)
names(New_Data1960)
New_Data2013 <- merge(P2_2013,yr2013, by.x = "Country.Code", by.y = "Country_Code")
head(New_Data1960)
#create scatter plot 1960
# plot(data=stats, x= Internet.users,y=Birth.rate,
# colour=Income.Group,size=I(4))
plot(data=New_Data1960, x= Fertility.Rate, y= Life_Expectancy_At_Birth_1960, colour = Region)
Try:
//To convert the Fertility rate to numeric for showing continuous x-scale on graph
New_Data1960$Fertility.Rate <- as.numeric(New_Data1960$Fertility.Rate)
ggplot(New_Data1960, aes(Fertility.Rate, Life_Expectancy_At_Birth_1960, group=Region, color=factor(Region))) +
geom_point(aes(color=Region))
The first line tells gives details about the plot
and the second line actually plots it into the plot.
Output is as follows:
I have a dataset which is a 58196 long univariate time series, as read in from an nc file. The time format is "Hours since 1901-01-01 00:00:00" but when I plot in the native plotting, it just puts the number on the x-axis and not the dates. I would also really like to use ggplot2 to plot this data, which I am unsure of how to execute to get my forecast with dates on the x-axis.
Here is my current code and I have attached my forecast.
library(ncdf4)
library(forecast)
ncname <- "data"
ncfname <- paste(ncname, ".nc", sep="")
dname <- "pc"
print(ncin)
level <- ncvarget(ncin, "level")
nlevel <- dim(level)
time <- ncvar_get(ncin, "time")
timeunits <- ncatt_get(ncin, "time", "units")
ntime <- dim(time)
data_array <- ncvar_get(ncin, dname)
data_name <- ncatt_get(ncin, dname, "long_name")
dataunits <- ncatt_get(ncin, dname, "units")
fillvalue <- ncatt_get(ncin, dname, "_FillValue")
nc_close(ncin)
data_atlevel <- data_array[13, ] #Data at correct level
model <- Arima(data_atlevel, order=c(5, 0, 2))
datafcst <- forecast(model, level=95, h=10)
datafcst
This is the point forecast I get.
Point Forecast Lo 95 Hi 95
58197 -1.0771861 -1.233741 -0.92063154
58198 -1.0619501 -1.372808 -0.75109220
58199 -1.0679265 -1.506646 -0.62920653
58200 -1.0199718 -1.552383 -0.48756100
58201 -1.0440858 -1.710606 -0.37756579
58202 -0.9907978 -1.796984 -0.18461137
58203 -0.9618964 -1.891371 -0.03242174
58204 -0.8945481 -1.924210 0.13511352
58205 -0.8809432 -2.024227 0.26234020
58206 -0.8099497 -2.061208 0.44130892
Using dput(data_atlevel) here is the final snippet of my data array.
-0.0164826595000697, 0.104653384815459, 0.144666301411712,
0.406755420463853,
0.542331794386273, 0.772911688508443, 0.828654158311162,
1.00638835512443,
1.02792247457942, 1.01310671932778, 0.970892191725733,
1.04831366415597,
0.973069827884041, 0.876318047545758, 0.752430017949216,
0.763014654544908,
0.713777524308546, 0.761635798278535, 0.683255478853632,
0.778303948835296,
0.782141199251199, 0.782386316509802, 0.645595202862218,
0.727508430007767,
0.626212602439461, 0.504371813146744, 0.274250430414235,
0.221935768196244,
0.0955978809727627, -0.0117079372064144, -0.220506096426176,
-0.280678113337438, -0.453903395790332, -0.558297954401431,
-0.684971715619909,
-0.564071754625509, -0.433913981524223, -0.344838175285692,
-0.276691358483194,
-0.0548432651627894, 0.0987101422673561, 0.161095707907232,
0.192603198478924,
0.37047216630912, 0.596148685775624, 0.709837093562596,
0.747193326805386,
0.834313129573062, 0.846131581908811, 0.725875974537757,
0.513447772481864,
0.481781009489498, 0.500408010535818, 0.493558364249431,
0.442018049583641,
0.563841476333714, 0.512644345356563, 0.406415545768519,
0.304138546560665,
0.344371236404124, 0.318640232058254, 0.288404684269042,
0.148595436033634,
0.242808391810353, 0.314848026042242, 0.312313203476422,
0.237498989259957,
0.40957356897388, 0.481207219937924, 0.377373174821711,
0.262952160510882,
0.291153919328814, 0.217630757477135, 0.221005490614492,
0.183186952148093,
0.259937980564005, 0.249506614072888, 0.183332972502036,
0.0364441950508707,
0.053725551911807, 0.0693769134725313, -0.0808066792116259,
-0.18171813264877,
-0.0493799795907768, 0.0445452522186435, 0.122164325736002,
0.212984374525893,
0.29001342164436, 0.298053187322413, 0.291905411666604,
0.195592541780216,
0.267534318598644, 0.368070820767661, 0.328049383662298,
0.194165510823487,
0.260603283301608, 0.342147730543151, 0.31173310207737,
0.355312158043337,
0.571008261101836, 0.706944792543549, 0.82879806488094,
0.974436043310877,
1.14302880864744, 1.20557808113165, 1.21299356275298,
1.17815791170924,
1.28295658294446, 1.19760427504148, 1.04365225746354,
0.899849906805248,
0.892025832204036, 0.805459364633312, 0.726511050764506,
0.718865825514464,
0.970667434937238, 1.17770847391413, 1.24344799640867,
1.19950652463226,
1.16726900972386, 1.03832992303071, 0.798481676817567,
0.620341026264972,
0.595943630221845, 0.56625911849429, 0.535218489717758,
0.48156033792909,
0.555048357972321, 0.555523556583205, 0.436775498179143,
0.335189690873195,
0.335735990769063, 0.156197092072704, -0.109413942180462,
-0.309332840349659,
-0.423112123260487, -0.582305168516083, -0.733093879897233,
-0.878533966114525,
-0.913591482716756, -0.927739936050881, -1.00944828473482,
-0.954596475363381,
-0.65373712144728, -0.476517683595976, -0.422610668989477,
-0.430362132554042,
-0.351913432549997, -0.270696606789033, -0.186484295363074,
-0.099740139097897,
0.029704154069367, 0.0664090863070563, -0.150476314540677,
-0.396841710776302,
-0.486497118842713, -0.593497285303129, -0.705987971623734,
-0.781265400572687,
-0.762280716228769, -0.896848223341563, -0.941459725854574,
-1.00367829312857,
-0.9908477396666)
My previous ggplot2 code was
library(ggplot2)
dates <- as.Date(time/24, origin="1900-01-01") #Interval is 6hrs
data_frame <- data.frame(datafcst, dates)
ggplot(data_frame, aes(x=dates, y=datafcst) +geompoint())
This gives me errors as the sizes aren't the same, but all I want is all my data+forecast data plotted for each time and the forecasted times.
I would like to be able to plot this forecast with the previous observed data to get a ggplot with dates on the x-axis. I am pretty stuck so any help would be great.
Many thanks!