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

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.

Related

Error in as.data.frame.default, class coercion 'structure("RasterStack", package = "raster")' in data.frame not possible

`Hello everybody
I get an error when I try to use the predict function. I'm doing a habitat suitability study.
this function requires the model (in this case a glmm) in which I have used trimmed variables, and I want to make the prediction on the same variables but not trimmed.
the varaibles i use in the model are the home range of animals for different climatic o ambient variables, i want predict on a biggest area. all variables are in the same extent, crs and spatial resolution
m
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) \['glmerMod'\] Family: binomial ( logit ) Formula: as.factor(pres.abs) \~ bio.3.kernel + bio.7.kernel + bio.8.kernel + bio.9.kernel + bio.13.kernel + bio.15.kernel + prec.7.kernel + landcover.kernel + slope.k + (1 | id) Data: new AIC BIC logLik deviance df.resid 21622\.34 21707.70 -10800.17 21600.34 17315 Random effects: Groups Name Std.Dev. id (Intercept) 0.1911 Number of obs: 17326, groups: id, 9 Fixed Effects: (Intercept) bio.3.kernel bio.7.kernel bio.8.kernel bio.9.kernel bio.13.kernel \-0.002928 -0.019971 -0.034466 0.310813 -0.325558 -0.324485 bio.15.kernel prec.7.kernel landcover.kernel slope.k \-0.671262 0.399149 0.142602 0.429925
names(var2)
`[1] "bio.3.kernel" "bio.7.kernel" "bio.8.kernel" "bio.9.kernel" "bio.13.kernel"
[6] "bio.15.kernel" "prec.7.kernel" "landcover.kernel" "slope.k"
p<- predict(m, var2)
Error in as.data.frame.default(data, optional = TRUE) : class coercion 'structure("RasterBrick", package = "raster")' in data.frame not possible

systemfit 3SLS Testing for Overidentification Restrictions

currently I'm struggling to find a good way to perform the Hansen/Sargan tests of Overidentification restrictions within a Three-Stage Least Squares model (3SLS) in panel data using R. I was digging the whole day in different networks and couldn't find a way of depicting the tests in R using the well-known systemfit package.
Currently, my code is simple.
violence_c_3sls <- Crime ~ ln_GDP +I(ln_GDP^2) + ln_Gini
income_c_3sls <-ln_GDP ~ Crime + ln_Gini
gini_c_3sls <- ln_Gini ~ ln_GDP + I(ln_GDP^2) + Crime
inst <- ~ Educ_Gvmnt_Exp + I(Educ_Gvmnt_Exp^2)+ Health_Exp + Pov_Head_Count_1.9
system_c_3sls <- list(violence_c_3sls, income_c_3sls, gini_c_3sls)
fitsur_c_3sls <-systemfit(system_c_3sls, "3SLS",inst=inst, data=df_new, methodResidCov = "noDfCor" )
summary(fitsur_c_3sls)
However, adding more instruments to create an over-identified system do not yield in an output of the Hansen/Sargan test, thus I assume the test should be executed aside from the output and probably associated to systemfit class object.
Thanks in advance.
With g equations, l exogenous variables, and k regressors, the Sargan test for 3SLS is
where u is the stacked residuals, \Sigma is the estimated residual covariance, and P_W is the projection matrix on the exogenous variables. See Ch 12.4 from Davidson & MacKinnon ETM.
Calculating the Sargan test from systemfit should look something like this:
sargan.systemfit=function(results3sls){
result <- list()
u=as.matrix(resid(results3sls)) #model residuals, n x n_eq
n_eq=length(results3sls$eq) # number of equations
n=nrow(u) #number of observations
n_reg=length(coef(results3sls)) # total number of regressors
w=model.matrix(results3sls,which='z') #Matrix of instruments, in block diagonal form with one block per equation
#Need to aggregate into a single block (in case different instruments used per equation)
w_list=lapply(X = 1:n_eq,FUN = function(eq_i){
this_eq_label=results3sls$eq[[eq_i]]$eqnLabel
this_w=w[str_detect(rownames(w),this_eq_label),str_detect(colnames(w),this_eq_label)]
colnames(this_w)=str_remove(colnames(this_w),paste0(this_eq_label,'_'))
return(this_w)
})
w=do.call(cbind,w_list)
w=w[,!duplicated(colnames(w))]
n_inst=ncol(w) #w is n x n_inst, where n_inst is the number of unique instruments/exogenous variables
#estimate residual variance (or use residCov, should be asymptotically equivalent)
var_u=crossprod(u)/n #var_u=results3sls$residCov
P_w=w%*%solve(crossprod(w))%*%t(w) #Projection matrix on instruments w
#as.numeric(u) vectorizes the residuals into a n_eq*n x 1 vector.
result$statistic <- as.numeric(t(as.numeric(u))%*%kronecker(solve(var_u),P_w)%*%as.numeric(u))
result$df <- n_inst*n_eq-n_reg
result$p.value <- 1 - pchisq(result$statistic, result$df)
result$method = paste("Sargan over-identifying restrictions test")
return(result)
}

How to fix "LoadError: DimensionMismatch ("cannot broadcast array to have fewer dimensions")"

I'd like to solve the following two coupled differential equations numerically:
d/dt Phi_i = 1 - 1/N * \sum_{j=1}^N( k_{ij} sin(Phi_i - Phi_j + a)
d/dt k_{ij} = - epsilon * (sin(Phi_i - Phi_j + b) + k_{ij}
with defined starting conditions phi_0 (1-dim array with N entries) and k_0 (2-dim array with NxN entries)
I tried this: Using DifferentialEquations.js, build a matrix of initial starting conditions u0 = hcat(Phi_0, k_0) (2-dim array, Nx(N+1)), and somehow define that the first equation applies to to first column (in my code [:,1]) , and the second equation applies to the other columns (in my code [:,2:N+1]).
using Distributions
using DifferentialEquations
N = 100
phi0 = rand(N)*2*pi
k0 = rand(Uniform(-1,1), N,N)
function dynamics(du, u, p, t)
a = 0.3*pi
b = -0.53*pi
epsi = 0.01
du[:,1] .= 1 .- 1/N .* sum.([u[i,j+1] * sin(u[i,1] - u[j,1] + a) for i in 1:N, j in 1:N], dims=2)
du[:,2:N+1] .= .- epsi .* [sin(u[i,1] - u[j,1] + b) + u[i,j+1] for i in 1:N, j in 1:N]
end
u0 = hcat(phi0, k0)
tspan = (0.0, 200.0)
prob = ODEProblem(dynamics, u0, tspan)
sol = solve(prob)
Running this lines of code result in this error:
LoadError: DimensionMismatch ("cannot broadcast array to have fewer dimensions")in expression starting at line 47 (which is sol = solve(prob))
I'm new to Julia, and I'm not sure if im heading in the right direction with this. Please help me!
First of all, edit the first package, which is Distributions and not Distribution, it took me a while to find the error xD
The main problem is the .= in your first equation. When you do that, you don't just assign new values to an array, you're making a view. I cannot explain you exactly what is a view, but what I can tell you is that, when you this kind of assign, the left and right side must have the same type.
For example:
N = 100
u = rand(N,N+1)
du = rand(N,N+1)
julia> u[:,1] .= du[:,1]
100-element view(::Array{Float64,2}, :, 1) with eltype Float64:
0.2948248997313967
0.2152933893895821
0.09114453738716022
0.35018616658607926
0.7788869975259098
0.2833659299216609
0.9093344091412392
...
The result is a view and not a Vector. With this syntax, left and right sides must have same type, and that does not happen in your example. Note that the types of rand(5) and rand(5,1) are different in Julia: the first is an Array{Float64,1} and the other is Array{Float64,2}. In your code, d[:,1] is an Array{Float64,1} but 1 .- 1/N .* sum.([u[i,j+1] * sin(u[i,1] - u[j,1] + a) for i in 1:N, j in 1:N], dims=2) is an Array{Float64,2}, that's why it doesn't work. You have two choices, change the equal sign for:
du[:,1] = ...
Or:
du[:,1] .= 1 .- 1/N .* sum.([u[i,j+1] * sin(u[i,1] - u[j,1] + a) for i in 1:N, j in 1:N], dims=2)[:,1]
The first choice is just a basic assign, the second choice uses the view way and matches the types of both sides.

Setting priors in rhierBinLogit function from basyem package

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.

Lua - Two local variables with the same name

I have been learning Lua and I was wondering if it is allowed to reference two local variables of the same name.
For example, in the following code segment, is the syntax legal (without undefined behavior)?
I ask because it does run, but I cannot seem to figure out what is happening behind the scenes. Is this simply referencing the same x local? Or are there now two local x variables that mess things up behind the scenes. I'd like to know what exactly is happening here and why it is the case.
local x = 5 + 3; -- = 8
local x = 3 - 2; -- = 1
print("x = " .. x); -- x = 1
There are two variables. The second shadows (but does not remove or overwrite) the first.
Sometimes you can still access the earlier definition via a closure.
local x = 5 + 3
local function getX1()
return x
end
local x = 3 - 2
local function getX2()
return x
end
print("x = " .. x); -- x = 1
print("x = " .. getX1()); -- x = 8
print("x = " .. getX2()); -- x = 1
All your local variables have been remembered by Lua :-)
local x = 5 + 3; -- = 8
local x = 3 - 2; -- = 1
local i = 0
repeat
i = i + 1
local name, value = debug.getlocal(1, i)
if name == 'x' then
print(name..' = '..value)
end
until not name
Yes, it is legal. Lua handles local-variable declarations as statements.
Here's an interesting example from the Lua Reference Manual:
Notice that each execution of a local statement defines new local variables. Consider the following example:
a = {}
local x = 20
for i=1,10 do
local y = 0
a[i] = function () y=y+1; return x+y end
end
The loop creates ten closures (that is, ten instances of the anonymous function). Each of these closures uses a different y variable, while all of them share the same x.
In this example, if ignore the returning closure part, there are 10 local variables named y in the same for block.