(SAS 9.4) Is any functions in SAS which can extract residual from regression equation? - function

I need help to know a function can extract residual from regression equation.
I need that function to make 2-stage credit model. I want to extract a residual from first stage model(regression) and apply the residual to second stage model(y value).
It will be very helpful if there is proper function in SAS 9.4.
thank you

Look at the documentation around PROC REG.
proc reg data=inData;
model y = x1 x2 x3;
output out=ouData r=resid;
run;
quit;
This takes data from the INDATA data set, regresses Y on X1, X2, and X3, and outputs the residuals in OUTDATA.

If you want to get fancy, you can do it 2-stage least squares with proc model.
proc model data=have;
exo x1 x2 x3;
endo y1 y2;
y1 = b1 + b2*y2 + b3*x1 + b4*x2;
y2 = b5 + b6*y1 + b7*x3;
fit y1 y2 / 2sls;
instruments _exog_;
run;

Related

What is the correct way to use DolphinDB function tmbeta?

I want to run the script in DolphinDB:
T = 1..25
X = 2..26
Y1 = 3..27
Y2 = 4..28
y_matrix = matrix(table(Y1 as y1, Y2 as y2))
y_matrix.rename!(1..25,`y1`y2).setIndexedMatrix!()
x_series = indexedSeries(1..25,X)
tmbeta(T=1..25, Y=y_matrix[0], X=x_series[0],window=10)
It returns an error: tmbeta(1 .. 25, y_matrix[0], x_series[0], 10) => Usage: tmbeta(T, X, Y, window). X must be a vector with the same length as T.
I’d like to calculate the coefficient estimate of an ordinary-least-squares regression of Y on X. What is the correct usage of tmbeta?
For function tmbeta, the parameter X (y_matrix[0] in the script) must be a vector. The error is caused by the incorrect data type of y_matrix[0].
typestr(y_matrix[0])
FAST INT MATRIX
As the y_matrix is an indexed matrix in DolphinDB, you can use mbeta instead of tmbeta as no index (T) is needed.
mbeta(X=x_series,Y=y_matrix,window=10)
The correct way to apply function tmbeta is:
y1_matrix=matrix(table(Y1 as y1, Y2 as y2))
tmbeta(T,X=x_series[0],Y=y1_matrix[0],window=10)
Here y1_matrix[0] is a vector:
typestr(y1_matrix[0])
FAST INT VECTOR

Matlab Writing function for rectangle

I have a homework, where I need to create a function in matlab which plots a rectangle. We can't use a rectangle function. We must define a own function.
I already coded some parts:
M=[1 2
1 6
4 6
4 2];
x1= M(1,1);
x2= M(2,1);
x3= M(3,1);
x4= M(4,1);
y1= M(1,2);
y2= M(2,2);
y3= M(3,2);
y4= M(4,2);
x= [x1 x2 x3 x4 x1];
y= [y1 y2 y3 y4 y1];
function plotrec = prec(x,y)
plot(x,y,'g-o');
axis([0 10 0 10])
end
But the function doesn't work.
Thanks!
It looks like you've already figured out what you needed, but in any case perhaps you will find this approach to the function to be helpful.
function plot_rec(M)
xmin = min(M(:,1))-1;
xmax = max(M(:,1))+1;
ymin = min(M(:,2))-1;
ymax = max(M(:,2))+1;
M = [M; M(1,:)];
plot(M(:,1),M(:,2),'g-o')
axis([xmin xmax ymin ymax])
end

Calling Octave interpolation function within function body

I'm trying to wrap Octave interpolation function in a function body,
function FUN = inter(p);
FUN = interpn (x1, x2, x3, x4, x5, A, p(1), p(2), p(3), p(4), p(5), "spline");
end
The reason why I'm doing this is because I'm using a package which function needs a string name function which would be in this case packageFunction("inter", argument1);
The issue is calling now for instance like,
disp("value = "), inter([10 2 4 3 4])
doesn't work; Doesn't see the vectors error: 'x1' undefined ,
Of course the vectors xi and matrix A are defined above the function body. Would appreciate advice on this,
thanks, Damir
------------- in file example1.m
[a b c] = fminuit('gaussian','mnplot',[10 166 33],[x;y;dy])
------------- in file gaussian.m
function f = gaussian(par,data);
%theoretical function
f = par(1)/(sqrt(2*pi)*par(3)) * exp(-.5*((data(1,:)-
par(2))./par(3)).^2);
if (size(data,1)==2), %chi-square, error = 1
f = sum((data(2,:) - f).^2);
elseif (size(data,1)>2), %chi-square, error = 3rd row of data
f = sum(((data(2,:) - f)./data(3,:)).^2);
end
Given you are using an old function that requires a string as the function, the first solution below will not work. This is, however, the right way to do it. Changing the old function to use function handles instead of strings would be my preferred solution. However, you can also use an alternative solution further down below, which uses global variables. This is not the recommended approach (we should strive to avoid globals), but will solve your near-term problems.
Correct approach: use an anonymous function
You should use an anonymous function, these can capture variables when they're defined:
inter = #(p)interpn (x1, x2, x3, x4, x5, A, p(1), p(2), p(3), p(4), p(5), "spline");
Now inter(p) works just as if inter had been declared as a normal function. But the values of x1, x2, etc as they were defined when inter was defined will be stored inside inter.
As stated, the function you pass inter to must be written to accept function handles.
Bad, quick solution: use global variables
First, create a file inter.m with the following contents:
function FUN = inter(p);
global x1 x2 x3 x4 x5 A
FUN = interpn (x1, x2, x3, x4, x5, A, p(1), p(2), p(3), p(4), p(5), "spline");
end
Next, in your function of script that calls inter, again declare the global variables (currently MATLAB warns that you should declare them as globals before giving them a value, in future versions this will be required):
global x1 x2 x3 x4 x5 A
x1 = ...
x2 = ...
% etc
inter([10 2 4 3 4])
% or:
fminuit('inter',...)

Gradient behavior in pytorch with multi-layer loss

I have a loss where each layer plays into the loss. Which is the correct approach in terms of making sure the weights are updated properly?
# option 1
x2 = self.layer1(x1)
x3 = self.layer2(x2)
x4 = self.layer3(x3)
In this option, I detach when feeding into each subsequent block
# option 2
# x2 = self.layer1(x1.detach())
# x3 = self.layer2(x2.detach())
# x4 = self.layer3(x3.detach())
shared ops which calculate 4 losses and sum them.
x4 = F.relu(self.bn1(x4))
loss = some_loss([x1, x2, x3, x4])
Option 1 is correct. When you detach a tensor, computation history/graph is lost and gradients won't be propogated to inputs/for computation done before detaching.
This can also be seen by this toy experiment.
In [14]: import torch
In [15]: x = torch.rand(10,10).requires_grad_()
In [16]: y = x**2
In [19]: z = torch.sum(y)
In [20]: z.backward()
In [23]: x.grad is not None
Out[23]: True
Using detach
In [26]: x = torch.rand(10,10).requires_grad_()
In [27]: y = x**2
In [28]: z = torch.sum(y)
In [29]: z_ = z.detach()
In [30]: z_.backward()
# this gives error
This is because when you call detach, it returns a new tensor with the values copied and information about previous computations is lost.

Convert a quadratic curve points to polynomial representation?

I have the X,Y of 2 end points and 1 bezier point, of a Quadratic Bezier curve.
Using this data, how can I derive the polynomial representation of the curve?
(source: euclidraw.com)
B(t) = (1-t) * (1-t) * B0 + 2 * (1-t) * t * B1 + t * t * B2
Oog. That would be tricky. Beziers are parametrized curves, namely:
x = f(t)
y = g(t)
where t=0 yields one endpoint and t=1 yields the other.
You could technically figure out how to eliminate "t" and get an equation in x and y, but it would not be a polynomial like y = a + bx + cx2 ...; it would be an equation h(x,y) = 0 where h is probably somewhat ugly.
Wikipedia has a section about this. Perhaps this helps.