R ksvm support vectors - r

I am trying to extract weights for R's ksvm package.
Usually I use the e1071 package and the weights can be computed by
weights = t(svmmodel$coefs) %*% svmmodel$SV
However, when I look into the ksvm package, both the coefficients and alphas (support vectors) are lists of the same dimension. The alphas do not return vectors.
My question is, how should I access the support vectors including the zero values? Would I have to use SVindex to correspond the variables back to the original input?
Thanks.

Use xmatrix in the ksvm model (https://www.rdocumentation.org/packages/kernlab/versions/0.9-29/topics/ksvm-class). The xmatrix slot of the ksvm model svmmodel can be accessed using svmmodel#xmatrix.

Related

Can dismo::evaluate() be used for a model fit with glmnet() or cv.glmnet()?

I'm using the glmnet package to create a species distribution model (SDM) based on a lasso regression. I've succesfully fit models using glmnet::cv.glmnet(), and I can use the predict() function to generate predicted probabilities for a given lambda value by setting s = lambda.min and type = "response".
I'm creating several different kinds of SDMs and had been using dismo::evaluate() to generate fit statistics (based on a testing dataset) and thresholds to convert probabilities to binary values. However, when I run dismo::evaluate() with a cv.glmnet (or glmnet) model, I get the following error:
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'as.matrix': not-yet-implemented method for <data.frame> %*%
This is confusing to me as I think the x argument in evaluate() isn't needed when I'm providing a matrix with predictor values at presence locations (p) and another matrix with values at absence locations (a). I'm wondering whether evaluate() doesn't work with these types of models? Thanks, and apologies if I've missed something obvious!
After spending more time on this, I don't think dismo::evaluate() works with glmnet objects when supplying "p" and "a" as matrices of predictor values. dismo::evaluate() converts them to data.frames before calling the predict() function. To solve my problem, I was able to create a new function based on dismo::evaluate() that supplies p or a as a matrix to the predict() function.

How to figure out the parameters from mppm in R

I am working using the spatstat library in R.
I have several point pattern objects built from my own dataset. The point patterns contain only the x and y coordinates of the points in them. I wanted to fit the point patterns to a Gibbs process with Strauss interaction to build a model and simulate similar point patterns. I was able to use ppm function for that purpose if I work with one point pattern at a time. I used rmhmodel function on the ppm object returned from the ppm function. The rmhmodel function gave me the parameters beta, gamma and r, which I needed to use in rStrauss function further to simulate new point patterns. FYI, I am not using the simulate function directly as I want the new simulated point pattern to have flexible number of points that simulate does not give me.
Now, if I want to work with all the point patterns I have, I can build a hyperframe of point patterns as described in the replicated point pattern chapter of the Baddeley textbook, but it requires mppm function instead of ppm function to fit the model and mppm is not working with rmhmodel when I am trying to figure out the model parameters beta, gamma and r.
How can I extract the fitted beta, gamma and r from a mppm object?
There are several ways to do this.
If you print a fitted model (obtained from ppm or from mppm) simply by typing the name of the object, the printed output contains a description of the fitted model including the model parameters.
If you apply the function parameters to a fitted model obtained from ppm you will obtain a list of the parameter values with appropriate names.
fit <- ppm(cells ~ 1, Strauss(0.12))
fit
parameters(fit)
For a model obtained from mppm, there could be different parameter values applying to each row of the hyperframe of data, so you would have to do lapply(subfits(model), parameters) and the result is a list with one entry for each row of the hyperframe, containing the parameters relevant to each row.
A <- hyperframe(Bugs=waterstriders)
mfit <- mppm(Bugs ~ 1, data=A, Strauss(5))
lapply(subfits(mfit), parameters)
Alternatively you can extract the canonical parameters by coef and transform them to the natural parameters.
You wrote:
I am not using the simulate function directly as I want the new simulated point pattern to have flexible number of points that simulate does not give me.
This cannot be right. The function simulate.mppm generates simulated realisations with a variable number of points. Try simulate(mfit).

LDA model on weighted data - R

I would like to use linear discriminant analysis model (lda) on my weighted data. In my data set, I have one column with weights which are not integers (I cant just replicate rows). lda function from MASS package does not allow me to use vector of weights for observations. Do you know, how deal with it ? I have tried also with mlr package but learner classif.lda still uses lda implementation from MASS package, so I get error:
Error in checkLearnerBeforeTrain(task, learner, weights) :
Weights vector passed to train, but learner 'classif.lda' does not support that!
Do you know how to solve this problem ?

What is the objective of model.matrix()?

I'm currently going through the 'Introduction to Statistical Learning' MOOC by Stanford OpenX. In one of the lab exercises, it suggests creating a model matrix from the test data by explicitly using model.matrix().
Extract from textbook
We now compute the validation set error for the best model of each model size. We first make a model matrix from the test data.
test.mat=model.matrix (Salary∼.,data=Hitters [test ,])
The model.matrix() function is used in many regression packages for
building an X matrix from data. Now we run a loop, and for each size i, we
extract the coefficients from regfit.best for the best model of that
size, multiply them into the appropriate columns of the test model
matrix to form the predictions, and compute the test MSE.
val.errors =rep(NA ,19)
for(i in 1:19){
coefi=coef(regfit .best ,id=i)
pred=test.mat [,names(coefi)]%*% coefi
val.errors [i]= mean(( Hitters$Salary[test]-pred)^2)
}
I understand that model.matrix would convert string variables into values with different levels, and that models like lm() would do the conversions under the hood.
However, what are the instances that we would explicitly use model.matrix(), and why?

How to extract the model matrix from a Julia GLM object

I'm playing around with linear regression in Julia using the GLM package. I am interested in getting the model matrix out from the LM object so I can calculate leverage values (diagonals of the hat matrix), but can't find any function to do this. The equivalent in R is model.matrix.
Any suggestions?
I guess I could just do the regression manually via matrix multiplication, but didn't want to reinvent the wheel.
Just figured this out, by calling names(OLS) on the LM object (which I am calling OLS). Then the model matrix can be extracted by OLS.mm.
If the data you're using is a DataFrame you can use the following:
using DataFrames, GLM
dat = dataset("car","Vocab")
x = ModelMatrix(ModelFrame(Vocabulary~Year+Sex+Education,dat)).m

Resources