I want to make a logistical map in Julia and I am having difficulties. I already know how to do it in Octave, how could I turn this code to Julia? My difficulty is mainly in the "map [i,:]" part.
#Creates a vector of initial conditions and "r"
x=rand(150,1);
r=(2.51:.01:4);
#Transpose the r
r=r';
#Makes 300 times the product of each element of r for each element of x
for i=1:300
x=r.*x.*(1-x);
end
#Makes 200 times the product of each element of r for each element of x and stores each value of x on line i of the "map" matrix
for i=1:200
x=r.*x.*(1-x);
map(i,:)=x;
end
#Plots each column of the map for each element o r
plot(r,map,'.')
Starting here:
#Creates a vector of initial conditions and "r"
x=rand(150,1);
This isn't a vector, but a matrix of size 150x1. If you want a vector, write:
x = rand(150)
As for this:
r=(2.51:.01:4);
#Transpose the r
r=r';
It's a bit unclear, but I think you want a vector of the same length as x, in that case you should not transpose it, because that turns it into a 1x150 matrix. So instead write
r = 2.51:0.01:4
# or, maybe better
r = range(2.51, 4; length=length(x))
Next:
#Makes 300 times the product of each element of r for each element of x
for i=1:300
x=r.*x.*(1-x);
end
Instead of creating a new variable x at each iteration, update x in-place, and remember to dot all the operators here:
for i in 1:300
x .= r .* x .* (1 .- x) # dot everything
end
I don't really understand what's going on with your code here, it seems to just do more of the same, a bit arbitrarily. More importantly, you try to update map before you have defined it. Furthermore, you shouldn't really use the name map because that's the name of an important built-in function in Julia.
#Makes 200 times the product of each element of r for each element of x and stores each value of x on line i of the "map" matrix
for i=1:200
x=r.*x.*(1-x);
map(i,:)=x;
end
Alternative. Pre-define a matrix, call it something other than map, and store the information column-wise, not row-wise, since that it much more efficient, both in Matlab/Octave and Julia
M = similar(x, length(x), 200)
for i in 1:200
x .= r .* x .* (1 .- x)
M[:, i] .= x # use square brackets, not parens
end
BTW: your code becomes much more readable if you put in spaces between operators. Cramming everything together gives a messy appearance. Also, use indentation for readability. And drop all the semicolons.
I haven't written julia in a while so there may be more efficient ways of doing this now, but here is a more or less direct equivalent to your octave code.
using PyPlot
x = rand( 150, 1 );
r = reshape( 2.51:.01:4, (:, 1) );
for i in 1:300
global x
x = r .* x .* (1 .- x);
end
Map = Matrix{Float64}(undef, 200, 150);
for i in 1:200
global x, Map
x = r .* x .* (1 .- x);
Map[i:i,:] .= transpose(x);
end
for i in 1:length(r)
plot( r, Map[i,:], "." )
end
Related
I am using the example of calculating the length of the arc around a circle and the area under the arc around a circle based on the radius of the circle (r) and the angle of the the arc(theta). The area and the length are both based on r and theta, and you can calculate them simultaneously in python.
In python, I can assign two values at the same time by doing this.
from math import pi
def circle_set(r, theta):
return theta * r, .5*theta*r*r
arc_len, arc_area = circle_set(1, .5*pi)
Implementing the same structure in R gives me this.
circle_set <- function(r, theta){
return(theta * r, .5 * theta * r *r)
}
arc_len, arc_area <- circle_set(1, .5*3.14)
But returns this error.
arc_len, arc_area <- circle_set(1, .5*3.14)
Error: unexpected ',' in "arc_len,"
Is there a way to use the same structure in R?
No, you can't do that in R (at least, not in base or any packages I'm aware of).
The closest you could come would be to assign objects to different elements of a list. If you really wanted, you could then use list2env to put the list elements in an environment (e.g., the global environment), or use attach to make the list elements accessible, but I don't think you gain much from these approaches.
If you want a function to return more than one value, just put them in a list. See also r - Function returning more than one value.
You can assign multiple variables the same value as below. Even here, I think the code is unusual and less clear, I think this outweighs any benefits of brevity. (Though I suppose it makes it crystal clear that all of the variables are the same value... perhaps in the right context it makes sense.)
x <- y <- z <- 1
# the above is equivalent to
x <- 1
y <- 1
z <- 1
As Gregor said, there's no way to do it exactly as you said and his method is a good one, but you could also have a vector represent your two values like so:
# Function that adds one value and returns a vector of all the arguments.
plusOne <- function(vec) {
vec <- vec + 1
return(vec)
}
# Creating variables and applying the function.
x <- 1
y <- 2
z <- 3
vec <- c(x, y, z)
vec <- plusOne(vec)
So essentially you could make a vector and have your function return vectors, which is essentially filling 3 values at once. Again, not what you want exactly, just a suggestion.
I've tried a couple ways of doing this problem but am having trouble with how to write it. I think I did the first three steps correctly, but now I have to fill the vector z with numbers from y that are divisible by four, not divisible by three, and have an odd number of digits. I know that I'm using the print function in the wrong way, I'm just at a loss on what else to use ...
This is different from that other question because I'm not using a while loop.
#Step 1: Generate 1,000,000 random, uniformly distributed numbers between 0
#and 1,000,000,000, and name as a vector x. With a seed of 1.
set.seed(1)
x=runif(1000000, min=0, max=1000000000)
#Step 2: Generate a rounded version of x with the name y
y=round(x,digits=0)
#Step 3: Empty vector named z
z=vector("numeric",length=0)
#Step 4: Create for loop that populates z vector with the numbers from y that are divisible by
#4, not divisible by 3, with an odd number of digits.
for(i in y) {
if(i%%4==0 && i%%3!=0 && nchar(i,type="chars",allowNA=FALSE,keepNA=NA)%%2!=0){
print(z,i)
}
}
NOTE: As per #BenBolker's comment, a loop is an inefficient way to solve your problem here. Generally, in R, try to avoid loops where possible to maximise the efficiency of your code. #SymbolixAU has provided an example of doing so here in the comments. Having said that, in aid of helping you learn the ins-and-outs of loops and vectors, here's a solution which only requires a change to one line of your code:
You've got the vector created before the loop, that's a good start. Now, inside your loop, you need to populate that vector. To do so, you've currently got print(z,i), which won't really do too much. What you need to to change the vector itself:
z <- c( z, i )
Should work for you (just replace that print line in your loop).
What's happening here is that we're taking the existing z vector, binding i to the end of it, and making that new vector z again. So every time a value is added, the vector gets a little longer, such that you'll end up with a complete vector.
where you have print put this instead:
z <- append(z, i)
I am normally a maple user currently working with R, and I have a problem with correctly indexing variables.
Say I want to define 2 vectors, v1 and v2, and I want to call the nth element in v1. In maple this is easily done:
v[1]:=some vector,
and the nth element is then called by the command
v[1][n].
How can this be done in R? The actual problem is as follows:
I have a sequence M (say of length 10, indexed by k) of simulated negbin variables. For each of these simulated variables I want to construct a vector X of length M[k] with entries given by some formula. So I should end up with 10 different vectors, each of different length. My incorrect code looks like this
sims<-10
M<-rnegbin(sims, eks_2016_kasko*exp(-2.17173), 840.1746)
for(k in 1:sims){
x[k]<-rep(NA,M[k])
X[k]<-rep(NA,M[k])
for(i in 1:M[k]){x[k][i]<-runif(1,min=0,max=1)
if(x[k][i]>=0 & x[i]<=0.1056379){
X[k][i]<-rlnorm(1, 6.228244, 0.3565041)}
else{
X[k][i]<-rlnorm(1, 8.910837, 1.1890874)
}
}
}
The error appears to be that x[k] is not a valid name for a variable. Any way to make this work?
Thanks a lot :)
I've edited your R script slightly to get it working and make it reproducible. To do this I had to assume that eks_2016_kasko was an integer value of 10.
require(MASS)
sims<-10
# Because you R is not zero indexed add one
M<-rnegbin(sims, 10*exp(-2.17173), 840.1746) + 1
# Create a list
x <- list()
X <- list()
for(k in 1:sims){
x[[k]]<-rep(NA,M[k])
X[[k]]<-rep(NA,M[k])
for(i in 1:M[k]){
x[[k]][i]<-runif(1,min=0,max=1)
if(x[[k]][i]>=0 & x[[k]][i]<=0.1056379){
X[[k]][i]<-rlnorm(1, 6.228244, 0.3565041)}
else{
X[[k]][i]<-rlnorm(1, 8.910837, 1.1890874)
}
}
This will work and I think is what you were trying to do, BUT is not great R code. I strongly recommend using the lapply family instead of for loops, learning to use data.table and parallelisation if you need to get things to scale. Additionally if you want to read more about indexing in R and subsetting Hadley Wickham has a comprehensive break down here.
Hope this helps!
Let me start with a few remarks and then show you, how your problem can be solved using R.
In R, there is most of the time no need to use a for loop in order to assign several values to a vector. So, for example, to fill a vector of length 100 with uniformly distributed random variables, you do something like:
set.seed(1234)
x1 <- rep(NA, 100)
for (i in 1:100) {
x1[i] <- runif(1, 0, 1)
}
(set.seed() is used to set the random seed, such that you get the same result each time.) It is much simpler (and also much faster) to do this instead:
x2 <- runif(100, 0, 1)
identical(x1, x2)
## [1] TRUE
As you see, results are identical.
The reason that x[k]<-rep(NA,M[k]) does not work is that indeed x[k] is not a valid variable name in R. [ is used for indexing, so x[k] extracts the element k from a vector x. Since you try to assign a vector of length larger than 1 to a single element, you get an error. What you probably want to use is a list, as you will see in the example below.
So here comes the code that I would use instead of what you proposed in your post. Note that I am not sure that I correctly understood what you intend to do, so I will also describe below what the code does. Let me know if this fits your intentions.
# define M
library(MASS)
eks_2016_kasko <- 486689.1
sims<-10
M<-rnegbin(sims, eks_2016_kasko*exp(-2.17173), 840.1746)
# define the function that calculates X for a single value from M
calculate_X <- function(m) {
x <- runif(m, min=0,max=1)
X <- ifelse(x > 0.1056379, rlnorm(m, 6.228244, 0.3565041),
rlnorm(m, 8.910837, 1.1890874))
}
# apply that function to each element of M
X <- lapply(M, calculate_X)
As you can see, there are no loops in that solution. I'll start to explain at the end:
lapply is used to apply a function (calculate_X) to each element of a list or vector (here it is the vector M). It returns a list. So, you can get, e.g. the third of the vectors with X[[3]] (note that [[ is used to extract elements from a list). And the contents of X[[3]] will be the result of calculate_X(M[3]).
The function calculate_X() does the following: It creates a vector of m uniformly distributed random values (remember that m runs over the elements of M) and stores that in x. Then it creates a vector X that contains log normally distributed random variables. The parameters of the distribution depend on the value x.
Finding Peak in a dataset - using R
Hi
I saw this thread on stackexchange. I am not an R programmer (yet). But I would like to implement in the C. But being not familiar with R syntax, I am not able to understand the code. I know its creating arrays such as y.max and i.max but I am not sure the operations done and how its manipulating the arrays. Here are the four lines I am particularly interested in.
y.max <- rollapply(zoo(y.smooth), 2*w+1, max, align="center")
delta <- y.max - y.smooth[-c(1:w, n+1-1:w)]
i.max <- which(delta <= 0) + w
list(x=x[i.max], i=i.max, y.hat=y.smooth)
Some pointers to understanding these particular syntax will be helpful.
Here is a translation of that code. R often uses nested function calls that can be hard to understand if you don't know what each function does. To help with this, I separated some lines into multiple lines and stored the results in new variables.
# convert y.smooth to a zoo (time series) object
zoo_y.smooth <- zoo(y.smooth)
# divide the data into rolling windows of width 2*w+1
# get the max of each window
# align = "center" makes the indices of y.max be aligned to the center
# of the windows
y.max <- rollapply(zoo_y.smooth,
width = 2*w+1,
FUN = max,
align="center")
R subsetting can be very terse. c(1:w, n+1-1:w) creates a vector of numbers called toExclude. Passing that vector with the - to the subsetting operator [] selects all element of y.smooth except for those at the indices specified in toExclude. Omitting the - would do the opposite.
# select all of the elements of y.smooth except 1 to w and n+1-1 to w
toExclude <- c(1:w, n+1-1:w)
subset_y.smooth <- y.smooth[-toExclude]
# element-wise subtraction
delta <- y.max - subset_y.smooth
# logical vector the same length of delta indicating which elements
# are less than or equal to 0
nonPositiveDelta <- delta <= 0
So nonPositiveDelta is a vector like TRUE FALSE FALSE... with an element for each element of delta, indicating which elements of delta are non-positive.
# vector containing the index of each element of delta that's <= 0
indicesOfNonPositiveDeltas <- which(nonPositiveDelta)
indicesOfNonPositiveDeltas, on the other hand, is a vector like 1, 3, 4, 5, 8 containing the index of every element of the previous vector that was TRUE.
# indices plus w
i.max <- indicesOfNonPositiveDeltas + w
Finally, the results are stored in a list. A list is sort of like an array of arrays, where each element of the list can itself be another list or any other type. In this case, each element of the list is a vector.
# create a three element list
# each element is named, with the name to the left of the equal sign
list(
x=x[i.max], # the elements of x at indices specified by i.max
i=i.max, # the indices of i.max
y.hat=y.smooth) # the y.smooth data
Without seeing the rest of the code or a description of what it's supposed to be doing, I had to guess a bit, but hopefully this helps you out.
I'm really confused about how to properly concatenate julia arrays. I have an array (sim1.value) that is 4875x3x4. I would like to collapse it over the last dimension so that is is 19500x3.
vcat(sim1.value) and cat(3,sim1.value) don't give the result I want.
vcat(args) command is like an abbreviation for cat(1,args) as it concatenates the given args on the vertical axis (the 1st dimension of your array)
You can get more information on that topic following this link: http://docs.julialang.org/en/latest/manual/arrays/#concatenation
Therefore, you can find a solution without using the reshape function:
# Get the size of your data
x, y, z = size(data)
# Create a "result matrix" with the same number of columns, but no lines
result = similar(data, 0, y)
# For each layer, concatenate the layer verticaly with the "result matrix"
for i in 1:z
result = vcat(result, data[:,:,z])
end