Fanchart Color Scale - r

I have the following predictions which I obtained from library(vars). Lets call this vecm.pred
$price
fcst lower upper CI
[1,] 4956.787 4864.032 5049.543 92.75548
[2,] 4948.936 4844.545 5053.327 104.39064
[3,] 5089.440 4979.941 5198.939 109.49891
[4,] 5076.999 4939.429 5214.569 137.56992
[5,] 5000.012 4854.955 5145.068 145.05669
[6,] 5072.107 4910.435 5233.780 161.67272
$people
fcst lower upper CI
[1,] 2529.799 2417.699 2641.899 112.1000
[2,] 2498.627 2269.438 2727.817 229.1893
[3,] 2410.037 2116.672 2703.402 293.3648
[4,] 2418.197 2094.965 2741.429 323.2320
[5,] 2371.373 2028.816 2713.929 342.5561
[6,] 2289.163 1941.386 2636.939 347.7764
I am trying to use fanchart to show my forecasts below:
fanchart(vecm.pred, ylab = c("Price (€)","Volume"), main = c("Price","People"))
But I cannot get past the following issues:
1) How do I change the colors from the default grey scale to a heatmap of red to yellows?
2) How do I have alternative ylabs for my first and second plot? As my ylab function above just provides two y-axis names for each plot.

Related

How can I create a boxplot with whiskers?

I have created a plot with 3 boxplots, but my whiskers are not showing for one of them.
How can I make them show?
This is my data:
Class 3: 5.055052 3.028838 3.423485 6.434745 6.396239 4.114418
3.687380 2.633139 7.356185 5.736677 4.462504 7.137034
Class 4: 4.738094 21.736701 6.716363 10.306583 4.757640 6.265024
My code is as follows:
boxplot(hvol.concentration,class.3, class.4, ylab="8-OHdG Concentration (ng/ml)", main="Boxplot Distribution of 8-OHdG", ylim=c(0,25), pch=16, names=c("Control", "NYHA III", "NYHA IV"))
boxplot(c(4.738094, 21.736701, 6.716363, 10.306583, 4.757640, 6.265024), plot = FALSE)$stats
## [,1]
## [1,] 4.738094 <<== It's definitely there but the lower bound of the IQR is almost the same as min val
## [2,] 4.757640
## [3,] 6.490694
## [4,] 10.306583 <<== Upper bound of IQR == max val
## [5,] 10.306583
If you make the plot window bigger the grid size will be sufficient to see the lower IQR:
boxplot(
c(4.738094, 21.736701, 6.716363, 10.306583, 4.757640, 6.265024),
horizontal = TRUE
)

Plot R matrix columns according to column name

I would like to plot the following matrix x, so the column data are plotted according to their column name (i.e. 0.1, 0.2, etc.) on the x-axis.
> x
0.1 0.2 0.3 0.4 0.5
[1,] 5.000000e-01 5.000000e-01 5.000000e-01 5.000000e-01 0.5000000000
[2,] 2.500000e-02 5.000000e-02 7.500000e-02 1.000000e-01 0.1250000000
[3,] 2.437500e-03 9.500000e-03 2.081250e-02 3.600000e-02 0.0546875000
[4,] 2.431559e-04 1.881950e-03 6.113802e-03 1.388160e-02 0.0258483887
[5,] 2.430967e-05 3.756817e-04 1.822927e-03 5.475560e-03 0.0125901247
[6,] 2.430908e-06 7.510810e-05 5.458812e-04 2.178231e-03 0.0062158067
[7,] 2.430902e-07 1.502049e-05 1.636750e-04 8.693947e-04 0.0030885852
[8,] 2.430902e-08 3.004053e-06 4.909445e-05 3.474555e-04 0.0015395229
[9,] 2.430902e-09 6.008089e-07 1.472761e-05 1.389339e-04 0.0007685764
[10,] 2.430902e-10 1.201617e-07 4.418219e-06 5.556585e-05 0.0003839928
But when I use
plot(x, pch=20, ylim=c(0, 1))
I get the following: Plot of R matrix.
I want a plot, where x[1, 1] (i.e. 5.000000e-01) is plotted as a point on 0.1 on the x-axis and 0.5 on the y-axis.
set.seed(123)
mat<-matrix(rnorm(25),5,5)
colnames(mat)<-seq(0.1,0.5,length.out=5)
plot(x=matrix(rep(as.numeric(colnames(mat)),5), 5,5,byrow=T),y=mat)
here the first argument x will repeat the number on the x axis by 5, so 5 x 5 I'll get a matrix which will give the right x position to each y column.
matplot(x=matrix(rep(as.numeric(colnames(mat)),5), 5,5,byrow=T),y=mat)
Can also be used

Predict() new data into PCA space in R

After performing a principal component analysis of a first data set (a), I projected a second data set (b) into PCA space of the first data set.
From this, I want to extract the variable loadings for the projected analysis of (b). Variable loadings of the PCA of (a) are returned by prcomp(). How can I retrieve the variable loadings of (b), projected into PCA space of (a)?
# set seed and define variables
set.seed(1)
a = replicate(10, rnorm(10))
b = replicate (10, rnorm(10))
# pca of data A and project B into PCA space of A
pca.a = prcomp(a)
project.b = predict(pca.a, b)
# variable loadings
loads.a = pca.a$rotation
Here's an annotated version of your code to make it clear what is happening at each step. First, the original PCA is performed on matrix a:
pca.a = prcomp(a)
This calculates the loadings for each principal component (PC). At the next step, these loadings together with a new data set, b, are used to calculate PC scores:
project.b = predict(pca.a, b)
So, the loadings are the same, but the PC scores are different. If we look at project.b, we see that each column corresponds to a PC:
PC1 PC2 PC3 PC4 PC5 PC6 PC7 PC8
[1,] -0.2922447 0.10253581 0.55873366 1.3168437 1.93686163 0.998935945 2.14832483 -1.43922296
[2,] 0.1855480 -0.97631967 -0.06419207 0.6375200 -1.63994127 0.110028191 -0.27612541 -0.37640710
[3,] -1.5924242 0.31368878 -0.63199409 -0.2535251 0.59116005 0.214116915 1.20873962 -0.64494388
[4,] 1.2117977 0.29213928 1.53928110 -0.7755299 0.16586295 0.030802395 0.63225374 -1.72053189
[5,] 0.5637298 0.13836395 -1.41236348 0.2931681 -0.64187233 1.035226594 0.67933996 -1.05234872
[6,] 0.2874210 1.18573157 0.04358772 -1.1941734 -0.04399808 -0.113752847 -0.33507195 -1.34592414
[7,] 0.5629731 -1.02835365 0.36218131 1.4117908 -0.96923175 -1.213684882 0.02221423 1.14483112
[8,] 1.2854406 0.09373952 -1.46038333 0.6885674 0.39455369 0.756654205 1.97699073 -1.17281174
[9,] 0.8573656 0.07810452 -0.06576772 -0.5200661 0.22985518 0.007571489 2.29289637 -0.79979214
[10,] 0.1650144 -0.50060018 -0.14882996 0.2065622 2.79581428 0.813803739 0.71632238 0.09845912
PC9 PC10
[1,] -0.19795112 0.7914249
[2,] 1.09531789 0.4595785
[3,] -1.50564724 0.2509829
[4,] 0.05073079 0.6066653
[5,] -1.62126318 0.1959087
[6,] 0.14899277 2.9140809
[7,] 1.81473300 0.0617095
[8,] 1.47422298 0.6670124
[9,] -0.53998583 0.7051178
[10,] 0.80919039 1.5207123
Hopefully, that makes sense, but I'm yet to finish my first coffee of the day, so no guarantees.

Manual colour scale not working

I am using RBrewer to to manually colour my ggplot bar chart but i'm having no luck.
I create my colour palette of blues and then assign it to a function for it to ramp.
blues <- brewer.pal(9, "Blues")
blue_range <- colorRamp(blues)
I then plot my stacked bar chart, where I know i have 20 groups.
ggplot(Month.Summary, aes(x=Calendar.Month, y = Measure, fill = Groups)) + geom_bar(stat="Identity", position = "fill") +scale_fill_manual(values = blue_range(20))
I unfortunately get the following error:
Error: Insufficient values in manual scale. 20 needed but only 3
provided.
I'm using Groups as my fill, where I know there are 2 instances. I'm passing 20 to the blue_range function so i'm not sure why it's saying i'm only passing 3 colours.
The blue_range() function expects values between 0 and 1. To get the discrete palette, pass a sequence to this function:
> blue_range(seq(0, 1, length.out = 20))
[,1] [,2] [,3]
[1,] 247.00000 251.00000 255.0000
[2,] 236.47368 244.26316 251.6316
[3,] 225.94737 237.52632 248.2632
[4,] 215.68421 230.78947 244.8947
[5,] 205.57895 224.05263 241.5263
[6,] 193.78947 217.21053 237.5263
[7,] 176.94737 210.05263 231.6316
[8,] 160.10526 202.89474 225.7368
[9,] 139.21053 191.68421 220.9474
[10,] 117.73684 179.89474 216.3158
[11,] 98.36842 168.10526 210.6316
[12,] 81.10526 156.31579 203.8947
[13,] 64.26316 144.26316 197.1053
[14,] 50.36842 130.36842 189.9474
[15,] 36.47368 116.47368 182.7895
[16,] 25.10526 102.89474 173.1053
[17,] 14.57895 89.42105 162.5789
[18,] 8.00000 75.78947 148.2632
[19,] 8.00000 61.89474 127.6316
[20,] 8.00000 48.00000 107.0000
This should work in the ggplot() call -- not tested because you didn't provide a reproducible example.
Note that recent ggplot2 has scale_fill_distiller() which provides a similar functionality with a more convenient interface.

Reuse a HoltWinters model using new data

I'm trying to reuse a HoltWinters model previously generated in R. I have found a related entry here, but it does not seem to work with HoltWinters. Basically I have tried something like this:
myModel<-HoltWinters(ts(myData),gamma=FALSE)
predict(myModel,n.ahead=10)
#time to change the data
predict(myModel,n.ahead=10,newdata=myNewData)
When I try to predict using the new data I get the same prediction.
I would appreciate any suggestion.
You can use update:
mdl <- HoltWinters(EuStockMarkets[,"FTSE"],gamma=FALSE)
predict(mdl,n.ahead=10)
Time Series:
Start = c(1998, 170)
End = c(1998, 179)
Frequency = 260
fit
[1,] 5451.093
[2,] 5447.186
[3,] 5443.279
[4,] 5439.373
[5,] 5435.466
[6,] 5431.559
[7,] 5427.652
[8,] 5423.745
[9,] 5419.838
[10,] 5415.932
predict(update(mdl,x=EuStockMarkets[,"CAC"]),n.ahead=10)]
Time Series:
Start = c(1998, 170)
End = c(1998, 179)
Frequency = 260
fit
[1,] 3995.127
[2,] 3995.253
[3,] 3995.380
[4,] 3995.506
[5,] 3995.633
[6,] 3995.759
[7,] 3995.886
[8,] 3996.013
[9,] 3996.139
[10,] 3996.266
predict.HoltWinters doesn't have a newdata argument, which is why the data doesn't get replaced. This is because the prediction doesn't require any data – it is described entirely by the coefficients argument of the model.
m <- HoltWinters(co2)
m$coefficients #These values describe the model completely;
#adding new data makes no difference

Resources