How to calculate random numbers in R - r

I have a matrix with 26 columns. The values in each row sum up to 1:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,] 0.02105263 0.01052632 0.01052632 0.04210526 0.01052632 0.06315789 0.03157895 0.1789474 0.07368421 0.07368421 0.02105263
[2,] 0.00000000 0.01176471 0.01176471 0.00000000 0.01176471 0.18823529 0.09411765 0.1764706 0.15294118 0.07058824 0.01176471
[3,] 0.00000000 0.00000000 0.02941176 0.01470588 0.04411765 0.11764706 0.05882353 0.2058824 0.07352941 0.08823529 0.00000000
[,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22]
[1,] 0.04210526 0.04210526 0.05263158 0 0.03157895 0.02105263 0.00000000 0.04210526 0.01052632 0.05263158 0.02105263
[2,] 0.00000000 0.01176471 0.00000000 0 0.03529412 0.01176471 0.04705882 0.04705882 0.02352941 0.01176471 0.00000000
[3,] 0.02941176 0.02941176 0.02941176 0 0.05882353 0.01470588 0.02941176 0.02941176 0.02941176 0.01470588 0.00000000
[,23] [,24] [,25] [,26]
[1,] 0.06315789 0.03157895 0.03157895 0.02105263
[2,] 0.05882353 0.02352941 0.00000000 0.00000000
[3,] 0.02941176 0.01470588 0.00000000 0.05882353
I would like to alternate the values to make up some new data. This would mean changing every value in a row randomly to a value in the range of +- 5%, while still adding up to 1 with the rowsum.
So in column2 the 6th value is currently 0.18 and in the new data it should be somewhere between 0.171 and 0.189 (and plus 5%).
Alternatively, the value in the column should just be drawn from a normal distribution, but should not differ too much from the original value. Maybe more for large values like 0.18 and also for values which are smaller.
If the value is 0, it would be good to randomly decide whether it should stay at 0 or increase by a range between 5% or 10% (taking as the initial value something like 0.0001).
Is there an easy way to do this?

Well, the first thing you want to do is be able to generate new numbers. It can be done with rnorm(). You can supply it with the mean and standard deviation. The mean should be zero and sd somewhere around 0.02 or so. It would result in vast majority of generated numbers to be within 0.05 of the original number.
After that you want to re-scale back to a rowsum of 1, which is easily achieved by dividing all values with the sum of the whole row.
> (a <- 1:10)
[1] 1 2 3 4 5 6 7 8 9 10
> (a <- a / sum(a))
[1] 0.01818182 0.03636364 0.05454545 0.07272727 0.09090909 0.10909091 0.12727273 0.14545455 0.16363636 0.18181818
> (a <- a + rnorm(10, 0, 0.02))
[1] 0.01293189 0.06799608 0.03552480 0.08015437 0.07834294 0.07845255 0.11692691 0.13262836 0.15728399 0.16228330
> sum(a)
[1] 0.9225252
> sum(a / sum(a))
[1] 1
> a <- a / sum(a)
I'll leave it to you to figure out how to eliminate negative numbers and the 5% or 10% increase. But those are the tools you need.

Let your dataset be a matrix called data, then data * matrix(runif(prod(dim(data)),.95,1.05),nrow=nrow(data)) will give you data that is all +/- 5%.
If you don't want negative values you can wrap it all in an abs() since if a value can shift by 5% and be negative, the absolute value will always be within 5% of the original value still.
If you want to start with no 0 values then step one is data = data[which(data<=0)] = 0.001

Related

`stoch.sens()` produces different results across code structures

I am using the hudsonia dataset from popbio for a reprex. Assuming that I have 2 lists of projection matrices that I want to calculate stochastic growth rate sensitivity.
What I want R to do is to apply stoch.sens to each nested list, and give me a new nested list of sensitivity and elasticity for each list of the projection matrices.
library(popbio)
data("hudsonia")
library(purrr)
library(tidyverse)
hudson <- list(hudsonia[1:2], hudsonia[3:4])
I set.seed(500), but it doesn't help. These three versions did the job as demonstrated in https://rdrr.io/cran/popbio/man/stoch.sens.html, but they give different results. Fortunately, the results seem to be consistent. How do I know which one is the most accurate?
scenario1_stoch.sens1 <- hudson %>%
map(., ~{stoch.sens(.)})
scenario1_stoch.sens1[[1]]
$sensitivities
seed seedlings tiny small medium large
[1,] 0.03305056 2.771649e-05 0.0001287404 0.0001431969 0.0001538398 0.0001839186
[2,] 13.00203595 1.134659e-02 0.0452484230 0.0484751986 0.0478620603 0.0884144846
[3,] 26.78972167 2.334817e-02 0.0933100332 0.1000829077 0.0989421815 0.1816309641
[4,] 46.35258286 4.059941e-02 0.1627644750 0.1744453985 0.1728116142 0.3133614288
[5,] 60.17717120 5.218833e-02 0.2105849414 0.2261332506 0.2239088481 0.4059634764
[6,] 73.30117860 6.334906e-02 0.2570030445 0.2762777810 0.2737179892 0.4930947167
$elasticities
seed seedlings tiny small medium large
[1,] 0.016508753 0.00000000 0.0005893993 0.001738768 0.0034331970 0.009230784
[2,] 0.005200814 0.00000000 0.0001764688 0.000494447 0.0008998067 0.003739933
[3,] 0.000000000 0.01114408 0.0552075615 0.017941807 0.0063520008 0.000000000
[4,] 0.000000000 0.00000000 0.0359152049 0.077326821 0.0301908444 0.026247911
[5,] 0.000000000 0.00000000 0.0000000000 0.066648140 0.1172797856 0.034134910
[6,] 0.000000000 0.00000000 0.0000000000 0.007905494 0.0624854738 0.409207593
scenario1_stoch.sens2 <- map(hudson, ~{stoch.sens(.x)})
scenario1_stoch.sens2[[1]]
$sensitivities
seed seedlings tiny small medium large
[1,] 0.03403483 2.845839e-05 0.0001207616 0.0001396471 0.0001501461 0.0001875122
[2,] 13.00850282 1.128956e-02 0.0517530409 0.0564579344 0.0569689775 0.0809149273
[3,] 26.69896759 2.310674e-02 0.1057631968 0.1155037098 0.1166128499 0.1659382881
[4,] 45.93156321 4.011413e-02 0.1806746089 0.1973586368 0.1990428759 0.2871106860
[5,] 57.80866176 5.033736e-02 0.2282652316 0.2491555124 0.2513725389 0.3608284037
[6,] 69.50472834 6.057715e-02 0.2735822612 0.2987184500 0.3012894262 0.4344459633
$elasticities
seed seedlings tiny small medium large
[1,] 0.017000397 0.00000000 0.0005528708 0.0016956653 0.003350765 0.009411144
[2,] 0.005203401 0.00000000 0.0002018369 0.0005758709 0.001071017 0.003422701
[3,] 0.000000000 0.01102885 0.0632803733 0.0202711514 0.007591172 0.000000000
[4,] 0.000000000 0.00000000 0.0392556049 0.0875884490 0.034936656 0.029256857
[5,] 0.000000000 0.00000000 0.0000000000 0.0739450382 0.132775014 0.037177426
[6,] 0.000000000 0.00000000 0.0000000000 0.0093164903 0.066822493 0.344268758
scenario1_stoch.sens3 <- stoch.sens(hudsonia[1:2])
$sensitivities
seed seedlings tiny small medium large
[1,] 0.03084729 0.0000258864 0.0001033805 0.0001115822 0.0001068774 0.0002138166
[2,] 12.76976503 0.0111371273 0.0541500215 0.0584835187 0.0602080777 0.0763170039
[3,] 26.19161727 0.0227584944 0.1105507047 0.1193949501 0.1227492926 0.1570462498
[4,] 45.34229432 0.0395745196 0.1895626453 0.2051304050 0.2107298137 0.2728195593
[5,] 56.73178410 0.0493258466 0.2361536206 0.2552820940 0.2616984321 0.3432671780
[6,] 68.02535325 0.0591084382 0.2809327390 0.3038881720 0.3110907601 0.4133270712
$elasticities
seed seedlings tiny small medium large
[1,] 0.015408220 0.00000000 0.0004732968 0.0013548868 0.002385150 0.010731348
[2,] 0.005107906 0.00000000 0.0002111851 0.0005965319 0.001131912 0.003228209
[3,] 0.000000000 0.01086263 0.0670680736 0.0204936687 0.008178951 0.000000000
[4,] 0.000000000 0.00000000 0.0401334404 0.0911770528 0.037510314 0.029048719
[5,] 0.000000000 0.00000000 0.0000000000 0.0762261032 0.139968147 0.036758280
[6,] 0.000000000 0.00000000 0.0000000000 0.0100422826 0.066522076 0.325381617

convert pictures to pixels dataframe in R (EBImage lib)

I have folder with images
C:/Users/admin/Downloads/mypicture
Here several images, I need read it.
I use library("EBImage")
x <- readImage("C:/Users/admin/Downloads/mypicture")
then get the error
Error in readImage("C:/Users/admin/Downloads/mypicture") :
Unable to determine type of C:/Users/admin/Downloads/mypicture: Filename extension missing.
How to read all pictures at once,
and then extract pixel array for each picture
# width and height of the original image
dim(x)[1:2]
# scale to a specific width and height
y <- resize(x, w = 200, h = 100)
# scale by 50%; the height is determined automatically so that
# the aspect ratio is preserved
y <- resize(x, dim(x)[1]/2)
# show the scaled image
display(y)
# extract the pixel array
z <- imageData(y)
How to do it?
here two images from my folder
I need to create pixel array
z <- array(y)
like this,
where marked name of picture kBFrf.jpg
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 0.4676471 0.5000000 0.5382353 0.5803922 0.6107843 0.6441176 0.6715686 0.6843137 0.6901961 0.7117647 0.7303922 0.7362745
[2,] 0.4735294 0.5078431 0.5441176 0.5745098 0.6186275 0.6392157 0.6774510 0.6882353 0.7127451 0.7245098 0.7500000 0.7764706
[3,] 0.4735294 0.5039216 0.5470588 0.5892157 0.6186275 0.6539216 0.6764706 0.7049020 0.7225490 0.7343137 0.7637255 0.7862745
[,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
[1,] 0.7686275 0.7843137 0.8000000 0.8156863 0.8450980 0.8460784 0.8519608 0.8539216 0.8529412 0.8607843 0.8647059 0.8568627
[2,] 0.8049020 0.8274510 0.8392157 0.8745098 0.8843137 0.8892157 0.8960784 0.8882353 0.8911765 0.9009804 0.8950980 0.8872549
[3,] 0.8058824 0.8303922 0.8558824 0.8735294 0.8784314 0.8901961 0.8911765 0.8882353 0.8794118 0.8823529 0.8803922 0.8852941
69onL.jpg
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 0.4676471 0.5000000 0.5382353 0.5803922 0.6107843 0.6441176 0.6715686 0.6843137 0.6901961 0.7117647 0.7303922 0.7362745
[2,] 0.4735294 0.5078431 0.5441176 0.5745098 0.6186275 0.6392157 0.6774510 0.6882353 0.7127451 0.7245098 0.7500000 0.7764706
[3,] 0.4735294 0.5039216 0.5470588 0.5892157 0.6186275 0.6539216 0.6764706 0.7049020 0.7225490 0.7343137 0.7637255 0.7862745
[,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
[1,] 0.7686275 0.7843137 0.8000000 0.8156863 0.8450980 0.8460784 0.8519608 0.8539216 0.8529412 0.8607843 0.8647059 0.8568627
[2,] 0.8049020 0.8274510 0.8392157 0.8745098 0.8843137 0.8892157 0.8960784 0.8882353 0.8911765 0.9009804 0.8950980 0.8872549
[3,] 0.8058824 0.8303922 0.8558824 0.8735294 0.8784314 0.8901961 0.8911765 0.8882353 0.8794118 0.8823529 0.8803922 0.8852941
How to create such dataframe
result for picture with digits
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19]
[1,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1.0000000 1.0000000
[2,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1.0000000 1.0000000
[,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29]
[1,] 1.00000000 1.00000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000
readImage function takes as input a vector of file names, so in order to read all images from a directory you need to list its contents
library(EBImage)
# create a vector of file names
fls <- dir("./img", full.names = TRUE)
# read images
imgs <- readImage(fls, 'png')
# display images
display(imgs, method = 'raster', all = TRUE)
Image data is stored in an Image class which internally holds an array
# internal representation of images data
y <- class(imageData(imgs))
# dimensions of image stack
dim(imgs)
## [1] 825 825 4 10
The first two dimensions correspond to the XY dimensions, the third one holds color channels (RGBA in this case), and the last one are the individual images. In order to extract a single image, for example the second one, use
img <- getFrame(imgs, 2, "render")
If you would like to refer to images by their original names you could use an approach similar to the following.
# mapping of file names to frame indices
idx <- setNames(1:length(fls), basename(fls))
getFrame(imgs, idx['sample.png'], "render")

Unexpected output containing plus, minus, and letters produced by subtracting one column of numbers from another in R

I have a data.frame containing a vector of numeric values (prcp_log).
waterdate PRCP prcp_log
<date> <dbl> <dbl>
1 2007-10-01 0 0
2 2007-10-02 0.02 0.0198
3 2007-10-03 0.31 0.270
4 2007-10-04 1.8 1.03
5 2007-10-05 0.03 0.0296
6 2007-10-06 0.19 0.174
I then pass this data through Christiano-Fitzgerald band pass filter using the following command from the mfilter package.
library(mFilter)
US1ORLA0076_cffilter <- cffilter(US1ORLA0076$prcp_log,pl=180,pu=365,root=FALSE,drift=FALSE,
type=c("asymmetric"),
nfix=NULL,theta=1)
Which creates an S3 object containing, among other things, and vector of "trend" values and a vector of "cycle" values, like so:
head(US1ORLA0076_cffilter$trend)
[,1]
[1,] 0.05439408
[2,] 0.07275321
[3,] 0.32150292
[4,] 1.07958965
[5,] 0.07799329
[6,] 0.22082246
head(US1ORLA0076_cffilter$cycle)
[,1]
[1,] -0.05439408
[2,] -0.05295058
[3,] -0.05147578
[4,] -0.04997023
[5,] -0.04843449
[6,] -0.04686915
Plotted:
plot(US1ORLA0076_cffilter)
I then apply the following mathematical operation in attempt to remove the trend and seasonal components from the original numeric vector:
US1ORLA0076$decomp <- ((US1ORLA0076$prcp_log - US1ORLA0076_cffilter$trend) - US1ORLA0076_cffilter$cycle)
Which creates an output of values which includes unexpected elements such as dashes and letters.
head(US1ORLA0076$decomp)
[,1]
[1,] 0.000000e+00
[2,] 0.000000e+00
[3,] 1.387779e-17
[4,] -2.775558e-17
[5,] 0.000000e+00
[6,] 6.938894e-18
What has happened here? What do these additional characters signify? How can perform this mathematical operation and achieve the desired output of simply $log_prcp minus both the $tend and $cycle values?
I am happy to provide any additional info that will help right away, just ask.

Extract and store a specific position from multiple matrices in an array in R

Sorry, newbie...
I've got an array object called "y" of 500 matrices of 6x6, like this:
, , 1
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.0000 0.3627 0.4132 0.4231 0.3795 0.5444
[2,] 0.3627 0.0000 0.2084 0.3523 0.2310 0.5377
[3,] 0.4132 0.2084 0.0000 0.1984 0.2920 0.4774
[4,] 0.4231 0.3523 0.1984 0.0000 0.2787 0.4363
[5,] 0.3795 0.2310 0.2920 0.2787 0.0000 0.5129
[6,] 0.5444 0.5377 0.4774 0.4363 0.5129 0.0000
[...]
, , 500
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.0000 0.3755 0.3568 0.3835 0.3582 0.5065
[2,] 0.3755 0.0000 0.0840 0.2253 0.2237 0.4066
[3,] 0.3568 0.0840 0.0000 0.1673 0.2434 0.4073
[4,] 0.3835 0.2253 0.1673 0.0000 0.2338 0.3403
[5,] 0.3582 0.2237 0.2434 0.2338 0.0000 0.4263
[6,] 0.5065 0.4066 0.4073 0.3403 0.4263 0.0000
I want to extract a specific position through all the 500 matrices in the array and store this 500 values in a vector named "unouno" for further analyses
I'm trying to do this:
for (i in 1:dim(y)[[3]]){
unouno<-y[2,1,i, drop=F]
}
but it only extracts the value for the last (500th) matrix.
(Once solved this I want to extract and store separately the 500 values of each of the 6 x 6 positions in the matrices)
We can do this by leaving the 3rd dimension blank
y[2,1,]
data
y <- array(1:60, dim=c(10,2,3))
If you would like to fix your loop, this could be one way to do it:
unouno <- NULL
for (i in 1:dim(y)[3]){
unouno[i]<-y[2,1,i]
}
It seems that you were mising indexing on the vector unouno as well

how to make R language result display in one line [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Increase the width of matrix printout
I have a big matrix named S, so it displays results separated across multiple lines. This is not clear for me, so is there a way to display the whole matrix, and not let the columns print on separated lines like this:
> S
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 1.0000000 0.5393599 0.5276449 0.33449680 0.2925090 0.60927180 0.2925090
[2,] 0.5393599 1.0000000 0.7826238 0.43412157 0.6507914 0.51639778 0.5423261
[3,] 0.5276449 0.7826238 1.0000000 0.41602515 0.5457052 0.50518149 0.5457052
[4,] 0.3344968 0.4341216 0.4160251 1.00000000 0.2690691 0.08006408 0.3363364
[5,] 0.2925090 0.6507914 0.5457052 0.26906912 1.0000000 0.42008403 0.5294118
[6,] 0.6092718 0.5163978 0.5051815 0.08006408 0.4200840 1.00000000 0.4900980
[7,] 0.2925090 0.5423261 0.5457052 0.33633640 0.5294118 0.49009803 1.0000000
[8,] 0.4029115 0.5378529 0.6013378 0.44474959 0.6482037 0.46291005 0.5185630
[9,] 0.3636364 0.5393599 0.5276449 0.16724840 0.6581452 0.52223297 0.4387635
[10,] 0.2727273 0.4045199 0.4522670 0.25087260 0.5850179 0.43519414 0.6581452
[11,] 0.4351941 0.6454972 0.5773503 0.32025631 0.4900980 0.41666667 0.4200840
[12,] 0.3636364 0.6741999 0.5276449 0.33449680 0.5850179 0.34815531 0.4387635
[13,] 0.1906925 0.2828427 0.1581139 0.43852901 0.3834825 0.18257419 0.3834825
[,8] [,9] [,10] [,11] [,12] [,13]
[1,] 0.4029115 0.3636364 0.2727273 0.4351941 0.3636364 0.1906925
[2,] 0.5378529 0.5393599 0.4045199 0.6454972 0.6741999 0.2828427
[3,] 0.6013378 0.5276449 0.4522670 0.5773503 0.5276449 0.1581139
[4,] 0.4447496 0.1672484 0.2508726 0.3202563 0.3344968 0.4385290
[5,] 0.6482037 0.6581452 0.5850179 0.4900980 0.5850179 0.3834825
[6,] 0.4629100 0.5222330 0.4351941 0.4166667 0.3481553 0.1825742
[7,] 0.5185630 0.4387635 0.6581452 0.4200840 0.4387635 0.3834825
[8,] 1.0000000 0.6446584 0.5640761 0.4629100 0.5640761 0.4225771
[9,] 0.6446584 1.0000000 0.6363636 0.3481553 0.4545455 0.2860388
[10,] 0.5640761 0.6363636 1.0000000 0.2611165 0.3636364 0.4767313
[11,] 0.4629100 0.3481553 0.2611165 1.0000000 0.5222330 0.2738613
[12,] 0.5640761 0.4545455 0.3636364 0.5222330 1.0000000 0.1906925
[13,] 0.4225771 0.2860388 0.4767313 0.2738613 0.1906925 1.0000000
In addition to the print(... ,digits) method you can also change the width at which printing wraps:
options(width = 150)
Make your window wide...
And, depending on the number of digits that count try...
print(S, digits = 3)
but you really need to come up with better ways of examining correlation matrices that don't depend on such things.

Resources