random number generation inside a vector - vector

Under scheme I want to generate a vector of random numbers, I have tried this like this:
(make-vector 10 (random 100))
and the output for this is such:
#(44 44 44 44 44 44 44 44 44 44)
so it seems like it uses the same seed for the generated items, how can I overcome this problem of generating n amount of randomly generated number sequence.
Cheers

If you are using PLT Scheme you can use build-vector:
(build-vector 100 (lambda (_) (random 100)))
If you are using one of the standardized versions (R5RS, R6RS etc.) you can define build-vector yourself, for example like this:
(define (build-vector n f)
(let ((v (make-vector n)))
(do ((i 0 (+ i 1))) ((> i 9) v)
(vector-set! v i (f i)))))

You need to call random 100 once for each element of the vector.

Another way to do it:
(define (random-vector count seed)
(let loop ((vec '(vector)) (i count))
(cond ((> i 0)
(loop (cons (random seed) vec) (sub1 i)))
(else (eval (reverse vec))))))
May not be the best solution, but shows how to write and evaluate a Scheme program at runtime. Maybe useful for new Schemers.
Some example usages:
> (random-vector 10 100)
#10(53 57 47 34 88 32 70 66 92 56)
> (random-vector 100 500)
#100(42 1 250 396 63 120 185 397 251 88 497 271 246 327 91 108 240 306 445 180 292 55 497 67 445 300 279 229 342 122 498 10 253 248 44 133 450 55 112 13 309 255 101 456 272 7 239 113 394 453 89 343 386 471 92 44 61 239 382 313 78 22 376 466 24 97 286 343 237 220 458 153 131 217 390 94 53 461 237 22 327 196 460 436 311 418 41 124 79 24 37 388 344 176 314 432 26 341 303 218)

Related

How to do a manipulations with datasets for which the name is listed in a vector?

I'd like to do several manipulations with datasets that are in-built in R from the packages that I have. So, first, I made a vector with dataset's names, but when I tried to filter the datasets which have only one column, I got an error, saying that the length of the argument is 0. Here is the code:
for (i in datasets){
if (ncol(i)==1){dataset <- i datasets <- c(dataset, datasets) }
}
It treats the names of the datasets as a character vector.
Here is the head of the aforementioned vector: [1] ability.cov airmiles AirPassengers airquality anscombe attenu. It's silly, but how could I treat the entries as dataframes?
I don't fully understand your logic, but based on your code, you want to identify which dataset that has one column by using ncol(x) == 1. If that's true, then you need to deal with some issues:
the various structures of the datasets. ncol produces the number of columns on data.frame and matrix but does not on time-series. For example: ncol(anscombe) results in 8 but ncol(AirPassengers) results in NULL. If you decide to use ncol, then you need to coerce each dataset to a data.frame by using as.data.frame.
indexing the character vector of the names of the datasets. You need to call a dataset, not its character name, to be able to use as.data.frame. One way of doing this is by using eval(parse(text=the_name)).
the way to store the result. You can use c() to combine the results but the datasets will be converted to vectors, no longer in their initial structures. I recommend using list to preserve the data frame structures of the datasets.
Here is one possible solution based on those considerations:
datasets <- c("ability.cov", "airmiles", "AirPassengers", "airquality", "anscombe", "attenu")
single_col_datasets <- vector('list', 1)
for (i in seq_along(datasets)){
if (ncol(as.data.frame(eval(parse(text = datasets[i])))) == 1){
single_col_datasets[[i]] <- as.data.frame(eval(parse(text = datasets[i])))
names(single_col_datasets[[i]]) <- datasets[i]
}
not.null.element <- single_col_datasets[lengths(single_col_datasets) != 0]
new.datasets <- list(not.null.element, datasets)
}
Here is the result:
new.datasets
[[1]]
[[1]][[1]]
airmiles
1 412
2 480
3 683
4 1052
5 1385
6 1418
7 1634
8 2178
9 3362
10 5948
11 6109
12 5981
13 6753
14 8003
15 10566
16 12528
17 14760
18 16769
19 19819
20 22362
21 25340
22 25343
23 29269
24 30514
[[1]][[2]]
AirPassengers
1 112
2 118
3 132
4 129
5 121
6 135
7 148
8 148
9 136
10 119
11 104
12 118
13 115
14 126
15 141
16 135
17 125
18 149
19 170
20 170
21 158
22 133
23 114
24 140
25 145
26 150
27 178
28 163
29 172
30 178
31 199
32 199
33 184
34 162
35 146
36 166
37 171
38 180
39 193
40 181
41 183
42 218
43 230
44 242
45 209
46 191
47 172
48 194
49 196
50 196
51 236
52 235
53 229
54 243
55 264
56 272
57 237
58 211
59 180
60 201
61 204
62 188
63 235
64 227
65 234
66 264
67 302
68 293
69 259
70 229
71 203
72 229
73 242
74 233
75 267
76 269
77 270
78 315
79 364
80 347
81 312
82 274
83 237
84 278
85 284
86 277
87 317
88 313
89 318
90 374
91 413
92 405
93 355
94 306
95 271
96 306
97 315
98 301
99 356
100 348
101 355
102 422
103 465
104 467
105 404
106 347
107 305
108 336
109 340
110 318
111 362
112 348
113 363
114 435
115 491
116 505
117 404
118 359
119 310
120 337
121 360
122 342
123 406
124 396
125 420
126 472
127 548
128 559
129 463
130 407
131 362
132 405
133 417
134 391
135 419
136 461
137 472
138 535
139 622
140 606
141 508
142 461
143 390
144 432
[[2]]
[1] "ability.cov" "airmiles" "AirPassengers" "airquality" "anscombe" "attenu"
You can use the get function:
for (i in datasets){
if (ncol(get(i))==1){
dataset <- i
datasets <- c(dataset, datasets)
}
}

How is the restricted mean upper limit in survival analysis calculated in R

I conducted Kaplan Meier analysis in R looking at he survival of fibres in a fatigue test. I have not predefined the upper limit for the restricted mean. How does R calculate or decide the upper limit in order to calculate the restricted mean?
I am using the following code:
fit = survfit(Surv(cyclicdata[,1], cyclicdata[,2]) ~ cyclicdata[,3])
print(fit, print.rmean=TRUE,rmean="common")
From experience with this calculation over the past few years, I believe that the restricted mean is by default calculated as the mean of the longest lives from each group.
For example, I have run into this with two groups having lives as below:
Group 1 Lives:
22 23 25 26 30 32 32 34 37 38 40 43 45 48 48 54
56 59 60 62 70 72 73 73 76 77 78 78 82 86 86 92
92 92 95 98 99 102 104 106 107 112 114 114 115 119 120 123
132 134 135 151 154 157 169 180
Group 2 Lives:
5 7 30 41 44 56 59 64 67 79 86 101 110 120 120 123
125 138 150 163 163 164 167 199 201 214 235 236 237 242 245 270
272 274 282 283 284 287 296 300 300 310 314 321 322 325 340 342
345 355 371 375 376 398 414 419 422 428 442 444 449 474 511 516 549
552 560 563 581 608 618 628 637 638 675 685 702 782 782 817 885
886 946 947 951
When I run my survival fit and print the output, I get this:
* restricted mean with upper limit = 566
This is equal to:
> mean(max(c(Group1$Lives,Group2$Lives)))
[1] 565.5
or
> (951+180)/2
[1] 565.5

inconsistency in a for loop with lists

I have downloaded the historical stock prices of a list of 218 stocks. I want check whether it is populated with the the most recent date or not. I have written a function to that effect, by name check.date
function(snlq){
j <- 1;
for(i in 1:length(snlq)){
ind <- index(snlq[[i]])
if(identical(ind[length(ind)],"2018-05-04") == FALSE){
s[j] <- i
j <- j+1
}
}
return(s);
}
snlq is list of stocks with length 218 and of class list
But when I run it, I get the following output:
check.date(snlq)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
[33] 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
[65] 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
[97] 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
[129] 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
[161] 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
[193] 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 356 358 359 360 361 362
[225] 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
[257] 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
[289] 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
[321] 459 460 461 462 463 464 465 466 467 468 469 470
How can the output be of length more than 218? Also I have checked that snlq[[1]] is up to date; then why is 1 in the output?
This might seem like a simple for loop problem, but is perplexing me.
Very many thanks for your time and effort...
It seems the problem is that s is not created in scope in which it is updated and used. #Dave2e has correctly pointed out in above comment. The most logical error seems to me is that s has been created in global space that's why your function is not giving error, otherwise your function would have not run.
There are many ways to fix the problem. One of the option can be as:
check.date <- function(snlq){
j <- 1;
ss <- integer() #declare before use in function scope
for(i in 1:length(snlq)){
ind <- index(snlq[[i]])
if(identical(ind[length(ind)],"2018-05-04") == FALSE){
s = c(s,j) #Kind of adding an element to vector s
j <- j+1
}
}
return(s);
}
I cannot check this result without a reproducible example, but I think this will simplify your function greatly.
check.data <- function(input, today) {
result <- sapply(input, function(x) {
ind <- index(x)
!identical(ind[length(ind)], today)
})
which(result)
}

Create SpatialLinesDataFrame from point coordinates

I am doing a animal tracking project. My data "finaltrimmed" looks like this
TrackIndex Time x_position y_position
1 1 0.1034 425 171
2 1 0.1379 425 169
3 1 0.1724 427 166
.........
125 25 1.1030 462 397
126 25 1.1380 462 397
127 25 1.1720 462 397
128 25 1.2070 462 397
129 25 1.2410 461 398
130 25 1.2760 462 399
131 25 1.3100 461 399
132 25 1.3450 461 399
133 25 1.3790 460 399
134 25 1.4140 460 399
.....
268 41 1.8280 302 280
269 41 1.8620 303 279
270 41 1.8970 302 280
271 41 1.9310 302 280
272 41 1.9660 302 281
273 41 2.0000 302 281
274 41 2.0340 302 281
275 41 2.0690 302 282
276 41 2.1030 302 282
277 41 2.1380 302 282
278 41 2.1720 302 283
........
I wish to create a line for each unique TrackIndex, which basically tracks how each individual insect move over time. And from there I want create a SpatialLinesDataFrame based on TrackIndex. Eventually, I want to use “buffer”function in “adehabitatMA” package to create a buffer area around each line.
I was able to create a SpatialPointsDataFrame using the following command.
xy<-cbind(finaltrimmed$x_position,finaltrimmed$y_position)
MatrixofPoints<-matrix(xy,ncol=2)
points<-SpatialPoints(MatrixofPoints)
dataframe=data.frame(finaltrimmed$TrackIndex)
df.points<-SpatialPointsDataFrame(points,dataframe)
However, I was not able to create a SpatialLinesDataFrame in a similar way.
My idea is to split the data frame “final trimmed” first with “split” function.
splitfinal<-split(finaltrimmed,finaltrimmed$TrackIndex)
which gives me the following data structure
$1
TrackIndex Time x_position y_position newindex
1: 1246 347.0 316 214 1
2: 1246 347.0 316 214 2
......
57: 1246 348.9 325 201 57
58: 1246 349.0 330 201 58
TrackIndex Time x_position y_position newindex
$25
TrackIndex Time x_position y_position newindex
1: 1318 363.6 375 422 1
2: 1318 363.7 375 422 2
.....
57: 1318 365.6 399 406 57
58: 1318 365.6 400 406 58
From there, I can cbind the x and y positions in “splitfinal” (this step didn’t work out because “splitfinal” is a list of lists). I am also not sure how to create a Lines-class, which is required to create a SpatialLinesDataFrame.
I am been stuck for many days and could not figure a way.
Can anyone help?
Here is an approach that should work:
Example data:
finaltrimmed <- read.table(text="TrackIndex Time x_position y_position
1 1 0.1034 425 171
2 1 0.1379 425 169
3 1 0.1724 427 166
130 25 1.2760 462 399
131 25 1.3100 461 399
132 25 1.3450 461 399
133 25 1.3790 460 399
134 25 1.4140 460 399
274 41 2.0340 302 281
275 41 2.0690 302 282
276 41 2.1030 302 282
277 41 2.1380 302 282
278 41 2.1720 302 283")
Solution:
library(raster)
ft <- split(finaltrimmed, finaltrimmed$TrackIndex)
z <- lapply(ft, function(i) spLines(as.matrix(i[, c('x_position', 'y_position')]), attr=data.frame(TrackIndex=i$TrackIndex[1])))
names(z) <- NULL
zz <- do.call(bind, z)

Is there a way to vectorize operations that access multiple elements of a vector?

Let's say I have a vector of integers:
> a<-sample(1:100,10)
> a
[1] 13 23 97 70 63 32 82 31 15 36
And I want a vector containing the cumulative values of this vector. That is, I want the vector
13 36 133 203 266 298 380 411 426 462
One way to achieve this would be to use a for loop. I prefer doing it using apply/lapply/sapply/..., but the only way I can think of for doing this is:
sapply(1:length(a), function(x) {sum(a[1:x])})
[1] 13 36 133 203 266 298 380 411 426 462
This works, but I was wondering if there was a better way to do this. Is there?
(This may have been a bad example, but in general, is there a way to access elements of the list being iterated over, given that you know the position of these element relative to the current one?)
You may giggle, but there's already a built in function for this, see ?cumsum
x <- sample(1:100,20)
x
[1] 32 42 54 79 92 69 96 41 51 22 74 76 86 37 85 99 3 11 17 57
cumsum(x)
[1] 32 74 128 207 299 368 464 505 556 578 652 728 814 851 936
[16] 1035 1038 1049 1066 1123

Resources