Skip to content

Commit

Permalink
update formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jdtuck committed Dec 16, 2023
1 parent e5f29fd commit 567ea14
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions fdasrsf/pcr_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ def calc_model(
lam = 0
R = 0
Phi = np.ones((N1, no + 1))
Phi[:, 1: (no + 1)] = self.pca.coef
Phi[:, 1 : (no + 1)] = self.pca.coef
xx = np.dot(Phi.T, Phi)
inv_xx = inv(xx + lam * R)
xy = np.dot(Phi.T, self.y)
b = np.dot(inv_xx, xy)
alpha = b[0]
b = b[1: no + 1]
b = b[1 : no + 1]

# compute the SSE
int_X = np.zeros(N1)
Expand All @@ -114,7 +114,7 @@ def calc_model(
SSE = np.sum((self.y - y_pred) ** 2)

self.Ntr = len(self.y)
stdev = np.sqrt(sum((y_pred - self.y)**2) / (self.Ntr - 2))
stdev = np.sqrt(sum((y_pred - self.y) ** 2) / (self.Ntr - 2))

self.alpha = alpha
self.b = b
Expand All @@ -124,14 +124,14 @@ def calc_model(

return

def predict(self, newdata=None, alpha=.05):
def predict(self, newdata=None, alpha=0.05):
"""
This function performs prediction on regression model on new
This function performs prediction on regression model on new
data if available or current stored data in object
Usage: obj.predict()
obj.predict(newdata)
:param newdata: dict containing new data for prediction (needs
:param newdata: dict containing new data for prediction (needs
the keys below, if None predicts on training data)
:type newdata: dict
:param f: (M,N) matrix of functions
Expand Down Expand Up @@ -224,11 +224,15 @@ def predict(self, newdata=None, alpha=.05):
raise Exception("Invalid fPCA Method")

self.y_int = np.zeros((n, 2))
X = inv(self.pca.coef.T@self.pca.coef)
X = inv(self.pca.coef.T @ self.pca.coef)
df = self.Ntr - self.pca.coef.shape[1] - 1
for ii in range(0, n):
self.y_pred[ii] = self.alpha + np.dot(a[ii, :], self.b)
interval = t.ppf(alpha/2, df)*self.stdev*np.sqrt(1+a[ii, :]@X@a[ii, :])
interval = (
t.ppf(alpha / 2, df)
* self.stdev
* np.sqrt(1 + a[ii, :] @ X @ a[ii, :])
)
self.y_int[ii, 0] = interval
self.y_int[ii, 1] = -interval

Expand All @@ -240,11 +244,15 @@ def predict(self, newdata=None, alpha=.05):
n = self.pca.coef.shape[0]
self.y_pred = np.zeros(n)
self.y_int = np.zeros((n, 2))
X = inv(self.pca.coef.T@self.pca.coef)
X = inv(self.pca.coef.T @ self.pca.coef)
df = self.Ntr - self.pca.coef.shape[1] - 1
for ii in range(0, n):
self.y_pred[ii] = self.alpha + np.dot(self.pca.coef[ii, :], self.b)
interval = t.ppf(alpha/2, df)*self.stdev*np.sqrt(1+self.pca.coef[ii, :]@X@self.pca.coef[ii, :])
interval = (
t.ppf(alpha / 2, df)
* self.stdev
* np.sqrt(1 + self.pca.coef[ii, :] @ X @ self.pca.coef[ii, :])
)
self.y_int[ii, 0] = interval
self.y_int[ii, 1] = -interval

Expand Down

0 comments on commit 567ea14

Please sign in to comment.