Setting priors in rhierBinLogit function from basyem package - bayesian

I checked the sample code from the appendix of bayesian stat and marketing the sample code used default prior which is
nu = nvar + 3
V = nu * diag(nvar)
Deltabar = matrix(rep(0, nz * nvar), ncol = nvar)
ADelta = 0.01 * diag(nz)
I just want to try a different prior setting and see how is look like so I change the deltabar to be
Deltabar = matrix(rep(10, nz * nvar), ncol = nvar)
Based on my understanding that set the prior believe of the deltabar to start from 10 so the MCMC of delta should begin in the value of 10 (please correct me if I am wrong).
However, I get same trace plot as default prior.

Related

Vegan package. db-RDA graph showed only the first 5 variables of the 20 total

I would like to see if the distribution of my species is affected by specific environmental parameters. When I try to do the analysis with the function "capscale ()" the graph showed only the first 5 variables of the 20 total. Please, can someone help me? Thanks.
This is the code I used.
species <- read.table('db-RDA.txt', sep ='\t', h = T, strip.white = T)
species = species[,2:ncol(species)]
species001= (species + 0.001)
fix(species001)
env <- read.table('Env Db-RDA.txt', sep ='\t', h = T, strip.white = T)
env = env[,-1]
env001= (env + 0.001)
fix(env001)
#I have not put all variables but only appeared the first 5 explanatory variables
dbRDA = capscale(species001 ~ Rock + Sand + Rubble + pH + NOx + NH4 + Temp + SIOH + DIC, data=env001, dist = "bray", scale=TRUE)
plot(dbRDA)
I guess that the function told you that some of your variables were aliased. Aliasing means that these variables are linear combinations of some other variables in the model, and there is no independent information to estimate those. See what the output says when you just call the short description of your result. You can get this by typing the name of the result as dbRDA.

Unexpected Rounding Error

can someone please explain the following to me?
Sub TestCalc()
Dim Z As Double
Dim Y As Double
Dim X As Integer
Dim W As Double
Dim V As Double
X = 44 / 14 ' returns 3
Z = (0.14 * 14) ' returns 1.96
Y = ((44 / 14) - (44 \ 14)) * 14 ' returns 2 SHOULD RETURN 1.96
W = (44 / 14) - X ' returns 0.142857142857143
V = W * 14 ' returns 2 SHOULD RETURN 1.96
End Sub
1.96 is the value that I would expect to get from the code. However, I only get this value when I use hard coded values. If I work with variables it rounds it up and returns the value 2 (Y or V). I'm need to understand why, as 1.96 is that value that I expect to be returned. I need to ensure that it performs this calculation correctly to ensure that my math formula functions properly in my main procedure
Your expectations are incorrect.
0.14 * 14 = 1.96; however, W is 0.142857142857143 - that value * 14 = 2.
I am guessing that there are some unseen conditions determining if the numbers you are entering are being calculated as integers or doubles. What happens when you type out Y as
Y = ((44.0 / 14.0) - (44.0 \ 14.0)) * 14.0
or however you can specify doubles in Visual Basic.
Also the bottom of this article mentions a mode that warns you when unsafe conversions take place which might help track it down.
https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/operators-and-expressions/arithmetic-operators

Solving a SHM (Simple Harmonic Motion) with given velocities at specific time

SHM - Simple Harmonic Motion
A simple harmonic oscillator has a frequency of 6.2 Hz. It is oscillating along x, where x(t) = Acos(wt + d). You are given the velocity at two moments: v(t=0) = 1.9 cm/s and v(t=.1) = -19.4 cm/s.
Calculate A and d.
Since I know that T = 1/f, I found out that T = 0.1612
And since, W = 2pi / T, w = 38.9557
Can't find out what to do with the velocities ?!
Need help as soon as possible...
You get
v(t) = -A*w*sin(w*t+d)
and you know v(0)=-Aw*sin(d) as well as v(t) for t=0.1.
v(t) = x'(t) = -A*w*( cos(w*t)*sin(d)+sin(w*t)*cos(d) )
so that
-A*w*cos(d) = ( v(t)-cos(w*t)*v(0) ) / sin(w*t)
and then conversion to polar coordinates will give you the values for A and d.

Matlab: how do I run the optimization (fmincon) repeately?

I am trying to follow the tutorial of using the optimization tool box in MATLAB. Specifically, I have a function
f = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1)+b
subject to the constraint:
(x(1))^2+x(2)-1=0,
-x(1)*x(2)-10<=0.
and I want to minimize this function for a range of b=[0,20]. (That is, I want to minimize this function for b=0, b=1,b=2 ... and so on).
Below is the steps taken from the MATLAB's tutorial webpage(http://www.mathworks.com/help/optim/ug/nonlinear-equality-and-inequality-constraints.html), how should I change the code so that, the optimization will run for 20 times, and save the optimal values for each b?
Step 1: Write a file objfun.m.
function f = objfun(x)
f = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1)+b;
Step 2: Write a file confuneq.m for the nonlinear constraints.
function [c, ceq] = confuneq(x)
% Nonlinear inequality constraints
c = -x(1)*x(2) - 10;
% Nonlinear equality constraints
ceq = x(1)^2 + x(2) - 1;
Step 3: Invoke constrained optimization routine.
x0 = [-1,1]; % Make a starting guess at the solution
options = optimoptions(#fmincon,'Algorithm','sqp');
[x,fval] = fmincon(#objfun,x0,[],[],[],[],[],[],...
#confuneq,options);
After 21 function evaluations, the solution produced is
x, fval
x =
-0.7529 0.4332
fval =
1.5093
Update:
I tried your answer, but I am encountering problem with your step 2. Bascially, I just fill the my step 2 to your step 2 (below the comment "optimization just like before").
%initialize list of targets
b = 0:1:20;
%preallocate/initialize result vectors using zeros (increases speed)
opt_x = zeros(length(b));
opt_fval = zeros(length(b));
>> for idx = 1, length(b)
objfun = #(x)objfun_builder(x,b)
%optimization just like before
x0 = [-1,1]; % Make a starting guess at the solution
options = optimoptions(#fmincon,'Algorithm','sqp');
[x,fval] = fmincon(#objfun,x0,[],[],[],[],[],[],...
#confuneq,options);
%end the stuff I fill in
opt_x(idx) = x
opt_fval(idx) = fval
end
However, it gave me the output is:
Error: "objfun" was previously used as a variable, conflicting
with its use here as the name of a function or command.
See "How MATLAB Recognizes Command Syntax" in the MATLAB
documentation for details.
There are two things you need to change about your code:
Creation of the objective function.
Multiple optimizations using a loop.
1st Step
For more flexibility with regard to b, you need to set up another function that returns a handle to the desired objective function, e.g.
function h = objfun_builder(x, b)
h = #(x)(objfun(x));
function f = objfun(x)
f = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1) + b;
end
end
A more elegant and shorter approach are anonymous functions, e.g.
objfun_builder = #(x,b)(exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1) + b);
After all, this works out to be the same as above. It might be less intuitive for a Matlab-beginner, though.
2nd Step
Instead of placing an .m-file objfun.m in your path, you will need to call
objfun = #(x)(objfun_builder(x,myB));
to create an objective function in your workspace. In order to loop over the interval b=[0,20], use the following loop
%initialize list of targets
b = 0:1:20;
%preallocate/initialize result vectors using zeros (increases speed)
opt_x = zeros(length(b))
opt_fval = zeros(length(b))
%start optimization of list of targets (`b`s)
for idx = 1, length(b)
objfun = #(x)objfun_builder(x,b)
%optimization just like before
opt_x(idx) = x
opt_fval(idx) = fval
end

Is *0.25 faster than / 4 in VB.NET

I know similar questions have been answered before but none of the answers I could find were specific to .NET.
Does the VB.NET compiler optimize expressions like:
x = y / 4
by compiling:
x = y * 0.25
And before anyone says don't worry the difference is small, i already know that but this will be executed a lot and choosing one over the other could make a useful difference in total execution time and will be much easier to do than a more major refactoring exercise.
Perhaps I should have mentioned for the benefit of those who live in an environment of total freedom: I am not at liberty to change to a different language. If I were I would probably have written this code in Fortran.
As suggested here is a simple comparison:
Dim y = 1234.567
Dim x As Double
Dim c = 10000000000.0
Dim ds As Date
Dim df As Date
ds = Now
For i = 1 To c
x = y / 4
Next
df = Now
Console.WriteLine("divide " & (df - ds).ToString)
ds = Now
For i = 1 To c
x = y * 0.25
Next
df = Now
Console.WriteLine("multiply " & (df - ds).ToString)
The output is:
divide 00:00:52.7452740
multiply 00:00:47.2607256
So divide does appear to be slower by about 10%. But this difference is so small that I suspected it to be accidental. Another two runs give:
divide 00:00:45.1280000
multiply 00:00:45.9540000
divide 00:00:45.9895985
multiply 00:00:46.8426838
Suggesting that in fact the optimization is made or that the arithmetic operations are a vanishingly small part of the total time.
In either case it means that I don't need to care which is used.
In fact ildasm shows that the IL uses div in the first loop and mul in the second. So it doesn't make the subsitution after all. Unless the JIT compiler does.