A value for the covariate “x” must be provided (as an argument to effectfun) - spatstat

I am reading text from spatstat textbook, and trying to learn model fit using ppm.
I created a model with carteisan coordinates as the covariates. And then I wanted to see the effect of only one covariate on the model,
model1 = ppm(chicago_ppp ~ x+y)
plot(effectfun(model1, covname = "x"))
but I get the error
Error in effectfun(model1, covname = "x") : A value for the covariate “y” must be provided (as an argument to effect fun)
The same happens if I use covname "y" it asks for "x"
Can someone please show me what is my mistake. Thank you.
UPDATE: When I use only one covariate, and I use effectfun with that one covariate, there is no error. When I use two covariates and I want to check effectfun of one covariate, I get this error in the question.

To be able to calculate the estimated intensity for different values of
x you need to fix a value for y like this:
library(spatstat)
model <- ppm(cells ~ x + y)
plot(effectfun(model, covname = "x", y = 0.1))
plot(effectfun(model, covname = "x", y = 0.9))

Related

Find global maximum of an equation using python

I am trying to write some codes to find the global maximum of an equation, e.g. f = -x**4.
Here is what I have got at the moment.
import sympy
x = sympy.symbols('x')
f = -x**4
df = sympy.diff(f,x)
ans = sympy.solve(df,x)
Then I am stuck. How should I substitute ans back into f, and how would I know if that would be the maximum, but not the minimum or a saddle point?
If you are just looking for the global maximum and nothing else, then there is already a function for that. See the following:
from sympy import *
x = symbols('x')
f = -x**4
print(maximum(f, x)) # 0
If you want more information such as the x value that gives that max or maybe local maxima, you'll have to do more manual work. In the following, I find the critical values as you have done above and then I show the values as those critical points.
diff_f = diff(f, x)
critical_points = solve(diff_f, x)
print(critical_points) # x values
for point in critical_points:
print(f.subs(x, point)) # f(x) values
This can be extended to include the second derivative test as follows:
d_f = diff(f, x)
dd_f = diff(f, x, 2)
critical_points = solve(d_f, x)
for point in critical_points:
if dd_f.subs(x, point) < 0:
print(f"Local maximum at x={point} with f({point})={f.subs(x, point)}")
elif dd_f.subs(x, point) > 0:
print(f"Local minimum at x={point} with f({point})={f.subs(x, point)}")
else:
print(f"Inconclusive at x={point} with f({point})={f.subs(x, point)}")
To find the global max, you would need to take all your critical points and evaluate the function at those points. Then pick the max from those.
outputs = [f.subs(x, point) for point in critical_points]
optimal_x = [point for point in critical_points if f.subs(x, point) == max(outputs)]
print(f"The values x={optimal_x} all produce a global max at f(x)={max(outputs)}")
The above should work for most elementary functions. Apologies for the inconsistent naming of variables.
If you are struggling with simple things like substitution, I suggest going through the docs for an hour or two.

How do i Square Root a Function in VBA

I am working on a MonteCarlo simulation model and part of it is to calculate the following formula:
X = Sqr(1-p)Y + Sqr(p)Z,
Where:
Y and Z are randomly obtained values based (idiosyncratic and systematic factors, respectviely) on a standard normal (inv.) distribution, calculated as:
Application.WorksheetFunction.NormInv (Rnd(), mean, sd)
p represents a correlation factor.
My aim is to square root a recalled formula, however when I try the following (inserting the first Sqr), it does not work and gives an error:
Matrix (n, sims) = (R * Sqr(Application.WorksheetFunction.NormInv(Rnd(), mean, sd))) + (Sqr(1 - R) * RandomS(s, x))
where:
R: Correlation factor
RandomS(s,x): generated matrix with Z values.
I don't want to go into too much details about the background and other variables, as the only problem I am getting is with Square Rooting the equation.
Error message I recieve reads:
Run-time error '5':
Invalid procedure call or argument
When I click debug it takes me to the formula, therefore there must be something wrong with the syntax.
Can you help with directly squaring the formula?
Thank you!
Andrew
Square root is simply Sqr.
It works fine in Excel VBA, so for example:
MsgBox Sqr(144)
...returns 12.
Just don't confuse it with the syntax for a worksheet function with is SQRT.
If you're still having an issue with your formula, tit must be with something other than the Square Root function, and I'd suggest you check the values of your variable, and make sure they are properly declared (preferably with Option Explicit at the top of the module).
Also make sure that you're passing Sqr a positive number.
Documentation: Sqr Function
I'm not a math major, but with your formula:
X = Sqr(1-p)Y + Sqr(p)Z,
...you specified how Y and Z are calculated, so calculate them separately to keep it simple:
Dim X as Double, Y as Double, Z as Double
Y = Application.WorksheetFunction.NormInv (Rnd(), mean, sd)
Z = Application.WorksheetFunction.NormInv (Rnd(), mean, sd)
Assuming the comma is not supposed to be in the formula, and having no idea what p is, your final code to calculate X is:
X = Sqr(1-p) * Y + Sqr(p) * Z

Error in caret-svm - "NAs are not allowed in subscripted assignments"

experts. I am a beginner to R. I am trying to use caret-SVM to make classification. The kernel is svmPoly.
First, I used the default parameters to train the model with leave-one-out cross-validation
The code is :
ctrl <- trainControl(method = "LOOCV",
classProbs = T,
savePredictions = T,
repeats = 1)
modelFit <- train(group~.,data=table_svm,method="svmPoly",
preProc = c("center","scale"),
trControl = ctrl)
The best accuracy is 80%. And the final values used for the model were degree = 1, scale = 0.1 and C = 1 .
Second, I tried to tune the parameters.
The code is:
grid_svmpoly=expand.grid(degree=c(1:11),scale=seq(0,5,length.out=25),C=10^c(0:4))
modelFit_tune <- train(group~.,data=table_svm,method="svmPoly",
preProc = c("center","scale"),
tuneGrid=grid_svmpoly,
trControl = ctrl)
I got an error message: Error in { :
task 264 failed - "NAs are not allowed in subscripted assignments"
I checked the data and found no NA.
There must be some NA inside the data-set. I am not new to this but not much expert. To ensure there is no NA inside first convert data-set into matrix format using:
x <- data.matrix(dataframe)
then use which() function which very handy in this case:
which(is.na(x)==T)
I hope this will help you finding the answer. The values will be in row wise order.
Let me know if this resolve your query.

How do I solve this exponential equation on Excel Solver?

100e^0.25*y = 97.5
Solving for y
Using Excel Solver
I tried using empty column entry for y in 'By changing cells' and Set objective function as LHS of above equation (empty column entry in equation included) equal to value of 97.5 in solver.
It gives no solution
How do I do this?
It's a bit ambiguous what you're asking...
Literal math interpretation: 100*(e^0.25)*y = 97.5
Then y = 97.5 / ( 100 * exp(.25)) = .759
My guess of what you want: 100*e^(0.25*y) = 97.5
Then y = ln(97.5/100) / .25 = -.101
Another possibility: (100 * e)^(0.25 * y) = 97.5
Then y = (ln(97.5) / ln(100*e)) / .25 = 3.268
Whatever it is, this doesn't need solver!
You don't really need the solver. Just re-arrange your formula to solve for Y. Since y = b^x is the same as log(b)Y = x (log of Y, with base b)
Your formula above is the same as:
Y = (log(100e)97.5))/.25
(Read aloud, that's log of 97.5, with base 100e, divided by .25
So, Y = 3.268305672
(Bonus points for someone who can tell me how to format this so the Log looks correct)
The question is "How do I solve this exponential equation on Excel Solver?" which is a fair enough question, as it points to trying to understand how to set up solver.
My interpretation of the equation provided is given in this screenshot ...
The solver dialog box is then setup as follows ...
Of note:
This is a non-linear equation and needs GRG Nonlinear. If you choose LP Simplex, it will not pass the linearity test.
Ensure "Make Unconstrained Variables Non-Negative" is not checked.
It provided this result for me ...
A more precise answer can be obtained by decreasing the "Convergence" value on the GRG Non-Linear Options dialog.
A problem this simple can also be solved using Goal Seek.

Matrix value not needed for Lop?

In the theano derivatives tutorial here:
http://deeplearning.net/software/theano/tutorial/gradients.html#tutcomputinggrads
the example of Lop works without an explicit value of the W matrix in the dot product. And, in fact, the partial derivatives in this case do remove the values of the components of W so they are not needed.
But, attempting a similar thing with the Rop throws an error:
theano.gof.fg.MissingInputError: ("An input of the graph, used to compute dot(Elemwise{second,no_inplace}.0, ), was not provided and not given a value.
How is this different?
Theano will try to optimize the computation graph, but it does not always work.
In the Lop example, Theano can detect that we don't actually need that W, but when changed to the Rop it just can't.
The Lop example:
W = T.dmatrix('W')
v = T.dvector('v')
x = T.dvector('x')
y = T.dot(x, W)
VJ = T.Lop(y, W, v)
f = theano.function([v, x], VJ)
f([2, 2], [0, 1])
If I just change y = T.dot(x, W) to y = T.dot(x, W**1), Theano will fail to do the optimization and throw the same error message at me say that I did not provide enough parameters.
Actually in the Rop example, if we change the values given to W, it does not affect the result at all, because Theano failed to optimize that.
p.s. I find the Theano documents very unclear sometimes.

Resources