The problem Ax=b for square A is solved by the \ function. With that in mind, I've tried to do the following:
A = rand(1:4,3,3)
x = fill(1.0, 3)
b = A * x
A\b
For some reason, the code seems to works at times. But sometimes it returns me the following error:
LinearAlgebra.SingularException(3)
Stacktrace:
[1] checknonsingular
# /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/factorization.jl:19 [inlined]
[2] checknonsingular
# /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/factorization.jl:21 [inlined]
[3] #lu!#136
# /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/lu.jl:85 [inlined]
[4] #lu#140
# /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/lu.jl:273 [inlined]
[5] lu (repeats 2 times)
# /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/lu.jl:272 [inlined]
[6] \(A::Matrix{Int64}, B::Vector{Float64})
# LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:1136
[7] top-level scope
# In[208]:4
[8] eval
# ./boot.jl:360 [inlined]
[9] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
# Base ./loading.jl:1116
So, I tried to understand what is happening, and executed the code 10000000 times and found out that it failed 10% of the times it was executed.
using Printf
i = 0
test = 10000000
for x in 1:test
try
A = rand(1:4,3,3)
x = fill(1.0, 3)
b = A * x
A\b
catch
i = i+1
end
end
fail_percentage = (i/test)*100
#printf "this code has failed in %.2f%%" fail_percentage
Can someone explain me what is happening here?
The error is explicit: LinearAlgebra.SingularException. This is not a failure of Julia, but a property of a system of equations.
There is no single solution if the matrix A is singular - either an infinite amount of solutions if the system is homogeneous, or none in the general case. Seems you have empirically calculated the probability of generating a singular system using the properties you tested (dimensions of A and x, x filled with 1s, A filled between 1 and 4).
In case, like OP, you are looking to skip singular matrices, you need to ensure determinant of A is not 0. You can either use the built in function to check and skip such matrices, or generate them by noting that the determinant is itself an equation, and so, say for your 3x3 example with no 0 entries, you choose 8 numbers, you can calculate what the 9th one cannot be to ensure the determinant is non-zero. If you allow for 0s you need to check all possibilities.
Related
It is the section of the Genetic algorithm that is coding in Julia. the code was wrote as follow:
popc= [individual(rand(0:1,nvar),[]) for i in 1:nc/4,j in 1:2];
for k=1:nc/4
#select firdt parent
i1=rand(1:npop);
p1=pop[i1];
#select second parent
i2=rand(1:npop);
if i1==i2
i2=rand(1:npop);
end
p2=pop[i2]
#apply crossover
m=singlepointcrossover(p1.position,p2.position);
append!(popc[k,1].position, m[1]);
append!(popc[k,2].position, m[2]);
end
function singlepointcrossover(x1,x2)
nvar=length(x1);
cutpoint=rand(1:nvar-1);
y1=append!(x1[1:cutpoint],x2[cutpoint+1:end]);
y2=append!(x2[1:cutpoint],x1[cutpoint+1:end]);
return y1,y2
end
but it has this error. would you pleas help me?. why is it happened?
ArgumentError: invalid index: 1.0
getindex(::Array{individual,2}, ::Float64, ::Int64) at abstractarray.jl:883
macro expansion at GA.juliarc.jl:87 [inlined]
anonymous at <missing>:?
include_string(::String, ::String) at loading.jl:522
include_string(::String, ::String, ::Int64) at eval.jl:30
include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N} where N) at eval.jl:34
(::Atom.##102#107{String,Int64,String})() at eval.jl:82
withpath(::Atom.##102#107{String,Int64,String}, ::String) at utils.jl:30
withpath(::Function, ::String) at eval.jl:38
hideprompt(::Atom.##101#106{String,Int64,String}) at repl.jl:67
macro expansion at eval.jl:80 [inlined]
(::Atom.##100#105{Dict{String,Any}})() at task.jl:80
The problem is / operator gives floating-point results for integer arguments and floating point results cannot be used for indexing an Array. You can index an Array with an Integer.
/(x, y)
Right division operator: multiplication of x by the inverse of y on the
right. Gives floating-point results for integer arguments.
for k=1:nc/4
1:nc/4 will create a Float64 range and k, a Float64, is later used in indexing in your code at append!(popc[k,1].position, m[1]);. You should, therefore, make k an Integer.
If nc is an integer, you should use Euclidean division with div(nc, 4) or simply nc ÷ 4, or bit shift operators nc >> 2 and nc >>> 2 (for Euclidean division by 2^n you should shift by n). They will all give integer results for integer arguments.
If nc itself is a floating-point number, you should probably use one of the options pointed out by #Colin T Bowers.
popc= [individual(rand(0:1,nvar),[]) for i in 1:nc/4,j in 1:2];
You do not have an error on the first line, since you do not use i for indexing here. It is still better to replace nc/4 with one of the options I listed above.
Fractions in Julia always output Float64, even if the answer can be exactly converted to Int.
Importantly, in your case, note that Int can be used to index arrays, but Float64 cannot. So you'll need to adjust:
for k=1:nc/2
to
for k=1:Int(nc/2)
so that your index k is will be of type Int, not Float64.
If nc is not guaranteed to be an even integer, then you may need to use floor(Int, nc/2) or ceil(Int, nc/2), depending on which is more appropriate.
I'm doing some ghetto parallelization in jags through rjags.
I've been using the function parallel.seeds to obtain RNG states to intialize the RNG's (example below). However, I don't understand why multiple integers are returned for each RNG. In the documentation it says that when you intialize .RNG.state is supposed to be a numeric vector with length one.
Furthermore, sometimes when I try to do this R crashes with no error generated. When I give up and just let it generate the seed for the chain on it's own, the model runs fine. Does this mean I am using the wrong .RNG.state? Any insight would be appreciated, as I am planning to scale up this model in the future.
> parallel.seeds("base::BaseRNG", 3)
[[1]]
[[1]]$.RNG.name
[1] "base::Wichmann-Hill"
[[1]]$.RNG.state
[1] 3891 16261 19841
[[2]]
[[2]]$.RNG.name
[1] "base::Marsaglia-Multicarry"
[[2]]$.RNG.state
[1] 408065014 1176110892
[[3]]
[[3]]$.RNG.name
[1] "base::Super-Duper"
[[3]]$.RNG.state
[1] -848274653 175424331
There is a difference between .RNG.seed (which is a vector of length one, and the thing you can specify to jags.model to e.g. ensure MCMC samples are repeatable) and .RNG.state (which is a vector of length depending on the pRNG algorithm). It is possible that these got mixed up in the docs somewhere - can you tell me where you read this so I can make sure it is fixed for JAGS/rjags 4?
Regarding the crashing - some more details would be needed to help you with that I'm afraid. I assume that it is the JAGS model that crashes, and not your R session that terminates, and after the model has been running for a while? A reproducible example would help a lot.
By the way - when you say 'scale up' - if you are planning to make use of > 4 chains I would strongly recommend you load the lecuyer module (see ?parallel.seeds examples at the bottom).
Matt
The documentation is a bit confusing; under ?jags.model we see that .RNG.seed should be a vector of length 1, but parallel.seeds() returns .RNG.state which is usually > 1. The state space for the Mersenne Twister algorithm has 624 integers, and that is the length of the vector when you do
parallel.seeds("base::BaseRNG",4)
to make sure you see all 4 types of RNG. Similarly the state space of the Wichmann-Hill generator has 3 integers, and I'm sure similar research would reveal the state spaces for the other two are longer than 1.
For my own edification I mocked up an example using the LINE data in rjags:
data(LINE)
LINE$model() ## edit and save to line.r
data = LINE$data()
line = jags.model("line.r",data=data)
line.samples <- jags.samples(LINE, c("alpha","beta","sigma"),n.iter=1000)
line.samples
inits = parallel.seeds("base::BaseRNG", 3) # a list of lists
inits[[1]]$tau = 1
inits[[1]]$alpha = 3
inits[[1]]$beta = 1
inits[[2]]$tau = .1
inits[[2]]$alpha = .3
inits[[2]]$beta = .1
inits[[3]]$tau = 10
inits[[3]]$alpha = 10
inits[[3]]$beta = 5
line = jags.model("line.r",data=data,inits=inits,n.chains=3)
line.samples <- jags.samples(line, c("alpha","beta","sigma"),n.iter=1000)
line2 = jags.model("line.r",data=data,inits=inits,n.chains=3)
line.samples2 <- jags.samples(line2, c("alpha","beta","sigma"),n.iter=1000)
all(line.samples$alpha-line.samples2$alpha < 0.00000001) ## TRUE
So the results are entirely repeatable, which is cool.
To understand the conditions under which R is crashing, I'd need to know the results of sessionInfo() on your computer, plus more details of the circumstances (e.g. what JAGS model are you running?). I just did:
for (i in 1:100){parallel.seeds("base::BaseRNG",4)}
and my computer didn't crash. For reference:
sessionInfo()
# R version 3.1.3 (2015-03-09)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 7 x64 (build 7601) Service Pack 1
#
# locale:
# [1] LC_COLLATE=English_United States.1252
# [2] LC_CTYPE=English_United States.1252
# [3] LC_MONETARY=English_United States.1252
# [4] LC_NUMERIC=C
# [5] LC_TIME=English_United States.1252
#
# attached base packages:
# [1] stats graphics grDevices utils datasets
# [6] methods base
#
# other attached packages:
# [1] rjags_3-14 coda_0.17-1 mlogit_0.2-4
# [4] maxLik_1.2-4 miscTools_0.6-16 Formula_1.2-1
#
# loaded via a namespace (and not attached):
# [1] grid_3.1.3 lattice_0.20-30 lmtest_0.9-33
# [4] MASS_7.3-39 sandwich_2.3-3 statmod_1.4.21
# [7] tools_3.1.3 zoo_1.7-12
That shows the version of R and rjags that I'm using.
I wanted to know how I can define variables in an implicit way in R.
For example, let's assume I have z<-0.5 and x<-2, I want to define y such that the following holds: z=beta(x,y).
Obviously, if I enter z<-beta(x,y), I have the following error Error in beta(x, y) : object 'y' not found.
I tried to find a solution in google but strangely I didn't find anything.
Thank you in advance!
For your example you could use uniroot to find the value of y:
(y <- uniroot(function(y) beta(x,y)-z, interval=c(0,100)))
$root
[1] 1
$f.root
[1] -1.08689e-07
$iter
[1] 13
$estim.prec
[1] 6.103516e-05
beta(x,y$root)==z
[1] FALSE
all.equal(beta(x,y$root),z, tol=1e-5)
[1] TRUE
beta(x,1)==z
[1] TRUE
However this relies on a number of assumptions such as there only being one value to satisfy the equation and you being able to give it a sensible interval. In general your function may not admit solutions, and it may be slow to compute if you need to calculate a large number of y values. You also need to consider that a numerical solution may not be exact, so comparisons will need to be made with care.
When you type a command in R say:
>5
[1] 5
it returns the integer 5 prefaced by [1]. Similar stuff happens when using a semicolon
>c(1,2,3); c(2,3,4)
[1] 1 2 3
[1] 2 3 4
If you assign 5 to a variable it does not return anything:
>a = 5
I think I understand R as a repl. If there is anything besides a variable assignment going on it prints that out prefaced by [1]
What confuses me is that doing something like this
>a = c(1,2,3)
>b = c(1,2,3)
>data = as.data.frame(cbind(a,b))
>model = lm(a ~ b, data)
>summary(model)
Call:
lm(formula = a ~ b, data = data)
Residuals:
1 2 3
0 0 0
.
.
.
Residual standard error: 0 on 1 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: 1
F-statistic: Inf on 1 and 1 DF, p-value: < 2.2e-16
My question is why is there no [1] in front of the summary of the model? In addition for emac's org mode to capture the results of a code block, the end of the code block seems to need to "return" something (i.e. the [1] prefaced in front of it). I read about the need to return something here. My idea right now is to capture the summary output in a PNG of some sort to display in my org mode block, however, I'd like to understand what's going on here and if there is a more basic hack. Also does anyone have any best practices for capturing the output of R model summaries in emac's org mode.
Thanks!!!!
UPDATE:
Using the 3 posts below I came up with:
#+begin_src R :results output :session R :exports results
library(ascii)
options(asciiType="org")
options(warn = -1)
a = c(1,2,3)
b = c(1,2,3)
data = as.data.frame(cbind(a,b))
model = lm(a ~ b, data)
ascii(summary(model))
#+end_src
I've wondered about this myself and went source code sleuthing.
The reason there is no '[1]' is that summary(model) returns an object of class summaryDefault which has a method SummaryDefault.print defined for it which gets called by the repl when it prints the summary info to the terminal and returns the summaryDefault object invisibly. So, the underlying vector of summary stats itself is not printed by print.default, which provides those indexes inside brackets.
However, the lack of the '[1]' is not the reason org is returning a blank result.
Here is the reason:
When you declare an org-mode code block with :returns value, Org mode tries to convert the value of the last expression in your code block into something that can be formatted as an org table (i.e. with | characters as delimiters).
It takes advantage of R's special variable .Last.value to get the value that needs to be converted.
It tries to convert this value to by calling the R function write.table on it, writing to a temp file which it then reads/parses/reformats into well formatted org table.
Alas, write.table, when called on an object of class SummaryDefault, generates an error, which gets caught and is silently un-reported, but that temp file winds up empty.
So there is nothing to reformat into an org table.
Your options today include:
OK: declare your code block instead with :returns output. Downside of this is that it will return ALL output, and the summary is not nicely reformatted into an org-table which maybe you want.
OK: leave it as returns :value, but make the last expression in your block be capture.output(summary(model)). The output will be a one column table, with one row for each line of output.
Better: leave it as returns :value, but make the last expression be unclass(summary(model)). Then the vector will get printed. Add a :rownames yes and we're looking pretty.
Betterer: leave it as returns :value, but use t(summary(model)) to simultaneously unclass the value and get it formatted wide.
Betterest: leave it as returns :value, but use t(t(summary(model))) to simultaneously unclass the value and keep it formatted skinny.
It is after all a reach to expect org to magically be able to convert any R data structure to a data.frame like thing and from there to an org table. I suppose org magic could catch the error and try something else, like calling t(t(.)) on the object it is trying to coerce to something tabular. Or as(.,data.frame). I'm not sure that's in the org "spirit". I wonder...
In any case, it would be good if org mode would raise an error, like, "Erhm, how do expect me to convert a summaryDefault object to a table".
Or?
There is a read-eval-print loop in R. Sometimes, however, the print method does not get anything if the method returns the value "invisibly" although that is not the case here. Furthermore, in the case of summary methods (which are generic) there is/are often specific print method(s) which will be associated with the generic summary function(s). These print functions then intervene and may instead print something at the console that is thought to be more easily digestible to human eyes that the nested list object that is actually the returned value. (So the print.summary.lm function uses cat, which works by side-effects and has no value, to "print" to the console and then returns the value ... invisibly.) In the simple case of:
a = 5
You can test for what is returned with:
b <- (a = 5)
b
[1] 5
So what is returned is the evaluated expression on the RHS of the assignment.
> d = 6
> e <- (b=d)
> e
[1] 6
If you type methods(print) you will see that there is a print.summary.lm* function that dispatched and handles the task of output to the console but what appears at the console is not the same as what you see with str(summary(model)). (The trailing asterisk means that the function will not be printed to the console if you just type its name, but you can get it to print with stats:::print.summary.lm.) You can also look at either:
?`<-`
?assign
I cannot really help with the Emacs part of the question. You may want to use either sink or capture.output to recover and store text identical to console output.
> cap <- capture.output(summary(model))
> print(cap)
[1] ""
[2] "Call:"
[3] "lm(formula = a ~ b, data = data)"
[4] ""
[5] "Residuals:"
[6] "1 2 3 "
[7] "0 0 0 "
[8] ""
[9] "Coefficients:"
[10] " Estimate Std. Error t value Pr(>|t|) "
[11] "(Intercept) 0 0 NA NA "
[12] "b 1 0 Inf <2e-16 ***"
[13] "---"
[14] "Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1"
[15] ""
[16] "Residual standard error: 0 on 1 degrees of freedom"
[17] "Multiple R-squared: 1,\tAdjusted R-squared: 1 "
[18] "F-statistic: Inf on 1 and 1 DF, p-value: < 2.2e-16"
[19] ""
[1] ([2], etc) is for showing values of vectors. It refers to the index of the leftmost value that is shown in this line. There are no scalars in R, that's why you see [1] even if you assign a single value to a variable. On the other hand, if you print something to standard output (say, using cat), that's not going to be the value that the function call returned (the result of cat is "invisible" NULL) but rather a side effect. Hence, no [1].
In order to use R with org-mode you need to decide what do you want to have in your document after a code block is executed: the last value, the output, or an image you code generated. It is controlled by :results header argument:
#+begin_src R :results value
a <- 5
#+end_src
Please see R Source Code Blocks in Org Mode for more details.
I am extremely new to R, so the solution to this is probably relatively simple. I have the following function to calculate stopping distance for an average car:
distance <- function(mph){(2.0*(mph/60))+(0.062673*(mph^1.9862))}
And I'm plotting all stopping distances from 1 mph to 60 mph:
range = distance(1:60)
But I need to mark where the stopping distance is equal to 120 ft. I don't have any idea how this is done in R, but I'd like to write a function where, for a stoppingdistance(x), I get the maximum speed of the car in MPH. What function should I use, and is there an easy way to check if the value of distance(x) (as it's written above) is equal to a certain value?
One way to do it would be to find when the function -120 is equal to 0:
distance <- function(mph, dist=0){(2.0*(mph/60))+(0.062673*(mph^1.9862))-dist}
uniroot(distance, c(1, 60), dist=120)
## $root
## [1] 44.63998
##
## $f.root
## [1] -5.088982e-06
##
## $iter
## [1] 6
##
## $estim.prec
## [1] 6.103516e-05
And to see if it worked:
distance(44.63998)
## [1] 120
I know this is an old question and has an accepted answer, but I want to point out another way to accomplish this - create the inverse function. Since you have the function, it is easy to generate many x-y points over a plausible range. Then simply create the inverse function by making y be a function of x. In your example,
x = seq(0,150,0.05)
y = distance(x)
speed = approxfun(y,x)
Now let's confirm that it gives good answewrs:
speed(120)
[1] 44.63998
distance(speed(120))
[1] 120