I have a question, if I am using SPSS, and I have an dependent variable, call it y, and ten independent variables, call them x1 through x10, is there a method to run a loop to check all possible combinations of five variables against the dependent variable, and get a summary of the R^2 values of the model. For instance:
y = independent; x1,x2,x3,x4,x5,x6,x7,x8,x9,x10 = dependent
Regression:
y, (x1,x2,x3,x4,x5)
y, (x1,x2,x3,x4,x6) ...
so on and so on checking all combinations?
Are you sure that you want to do this rather than using a procedure like stepwise regression or best subsets? What's the goal? You will get 252 regressions.
But here's a bit of Python code to do this. The spss.Submit line below should be indented.
begin program.
import spss, itertools
for v in itertools.combinations(['x1','x2','x3','x4','x5',\
'x6','x7','x8','x9','x10'], 5):
spss.Submit("""REGRESSION /DEPENDENT = y /ENTER=%s""" % " ".join(v))
end program.
Related
error: 'y' undefined near line 8 column 12
error: called from computeCost at line 8 column 3
Here is my code:
1;
function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost.
J = sum(( X * theta - y ) .^2 )/( 2 * m );
% =========================================================================
end
I am guessing it's an error from Coursera ML course assignment. I think you are trying to run the file which contains the implementation of function computeCost(X, y, theta), not the file which calls the computeCost(,,) function with values of X, y, theta. This is why you are getting the error as you aren't providing y.
Run the file which is calling computeCost() function, not the file which contains the implementation of computeCost() function.
That is:
For Week2 Assignment 1: Run ex1.m file
For Week3 Assignment 2: Run ex2.m file
There are two things happening here. First you are defining your function dynamically as opposed to in its own file; not sure why you would prefer that.
Second, after having defined this computeCost function, you are calling it from a context where you did not pass a y argument (or presumably, you didn't pass any arguments to it, and y happens to be the first one detected as missing inside the function).
Since this is a cost function and your code looks suspiciously like code from Andrew Ng's Machine Learning course on Coursera, I am going to go out on a limb here and guess that you called computeCost from something else that was supposed to use it as a cost function to be optimised, e.g. fminunc. Typically functions like fminunc expect a function handle as an argument, but they expect a very specific function handle too. If you look at the help of fminunc, it states that:
FCN should accept a vector (array) defining the unknown variables,
and return the objective function value, optionally with gradient.
Therefore, if you want to pass a function that is to be computed with three arguments, you need to "wrap" it into your own handle, which you can define on the spot, e.g. #(x) computeCost(x, y, t) (assuming 'y' and 't' exist already).
So, I'm guessing that instead of calling fminunc like so: fminunc( #(x) computeCost(x, y, t),
you probably called it like so: fminunc( #computeCost )
or even like so: fminunc( computeCost ) (which evaluates the function first, rather than pass a function handle as an argument).
Basically, go back to the code given to you by coursera, or read the notes carefully. You're calling things the wrong way.
Actually, you are trying to run a function and you can't run it until you provide the desired no. of parameters. Doing so, you may encounter the following error:
computeCost error: 'y' undefined near line 7 column 12
error: called from computeCost at line 7 column 3
As you see, here I'm calling this function without passing any argument.
SOLUTION:
You can test your code by running 'ex1' script. After that submit your work by calling 'submit' script.
Basically, I need to solve a function, ill use x^2 * y^3 for an example, but using the x and y values over a range of (0,2), with different steps for x values and for y values. For example, if the step was 1 for both x and y, I would just solve the function for (0,0),(0,2),(2,0), and (2,2).
I'm not sure how to accomplish this in Fortran without using arrays. I was thinking about using a "DO" and then solving the function for all of the initial "y" values, while using the correct amount of steps for "x," but im not sure how I would get the second "y" value with the "x" values reset so that the "y" can be calculated with all of the x values.
Thank you!
Here is a simple solution, based on do concurrent construct in modern Fortran (>2008) which is guaranteed to be equally or more efficient than the conventional do-loops in Fortran:
program hello
integer :: x,y
real :: stepSizeX = 0.1, stepSizeY = 0.2
do concurrent(x=0:2:1,y=0:2:1)
write(*,*) (stepSizeX*x)**2 * (stepSizey*y)**3
end do
end program Hello
You can test it here (set the compiler's Fortran standard to -std=f2008 or -std=gnu).
I've been trying to display in my console an exponential equation like the following one:
y(t) = a*e^t + b*e^t + c*e^t
I would write it as a string, however the coefficients a,b and c, are numbers in a vector V = [a b c]. So I was trying to concatenate the numbers with strings "e^t", but I failed to do it. I know scilab displays polynomial equations, but I don't know it is possible to display exponential one. Anyone can help?
Usually this kind of thing is done with mprintf command, which places given numerical arguments into a string with formatting instructions.
V = [3 5 -7]
mprintf("y(t) = %f*e^t + %f*e^t + %f*e^t", V)
The output is
y(t) = 3.000000*e^t + 5.000000*e^t + -7.000000*e^t
which isn't ideal, and can be improved in some ways by tweaking the formatters, but is readable regardless.
Notice we don't have to list every entry V(1), V(2), ... individually; the vector V gets "unpacked" automatically.
If you wanted to have 2D output like what we get for polynomials,
then no, this kind of thing is what Scilab does for polynomials and rational functions only, not for general expressions.
There is also prettyprint but its output is LaTeX syntax, like $1+s+s^{2}-s^{123}$. It works for a few things: polynomials, rational functions, matrices... but again, Scilab is not meant for symbolic manipulations, and does not really support symbolic expressions.
Is there a way to find the libsvm parameters (c, g, p) for REGRESSION in MATLAB?
It's ok to find them with gridgregression.py but what if we want to use them in Matlab? It is a bit timespend to export the train x and y matrix and find the parameteres via gridregression.py.
Write the cross-validation procedure manually. It's just 3 nested loops (over possible values of $c$, $g$ and $p$). In the inner loop you call svm-train in cross-validation mode (-v k) for k-fold cross-validation.
Save the best $(c,g,p)$-tuple you obtain and you are done.
Suppose I have a matrix A and I want to obtain the following:
for i=1:m
A(i,:) = something which depends on i;
endfor
Is there a way to obtain that without the loop?
Added: Ok, I've understood I have to be more specific.
I have two matrices B and C (all the matrices we are considering have m rows).
I want to record in the i-th row of A the product of the polynomials written in the i-th rows of B and C (so using the loop I would call the conv function).
Any ideas?
I don't think it's possible to do this without a for loop as conv only accepts vector inputs, but I might be wrong. I can't see a way of using either bsxfun or arrayfun in conjunction with conv and matrix inputs. I might be wrong though... I stand to be corrected.
That is a very very general question, and not possible to answer with more details. Mainly on what i will be involved with. Suppose the following
for i = 1:m
A(i,:) += i;
endfor
It could be written with the much more efficient:
A .+ (1:m)'
Just compare:
octave> n = 1000;
octave> A = B = rand (n);
octave> tic; for i = 1:n, B(i,:) += i; endfor; toc
Elapsed time is 0.051 seconds.
octave> tic; C = A.+ (1:n)'; toc
Elapsed time is 0.01 seconds.
octave> isequal (C, B)
ans = 1
If you have a very old version of octave, you can instead do bsxfun (#plus, A, (i:m)').
However, if i on the right side of the expression will be used for indexing some other variable, then solution would be different. Maybe, the solution is cumsum, or some other of cumfoo function.
Your question is basically, "how do I vectorize code?", which is a really large subject, without telling us what you're trying to vectorize.