

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
R tips for linear modeling, focusing on handling collinearity and backing up files in unix. It covers standardizing inputs, calculating means and standard deviations, and creating a function for standardizing data. The document also discusses what makells should return and collecting and combining multiple results.
Typology: Study notes
1 / 33
This page cannot be seen from the preview
Don't miss anything!


























Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
means <โ colMeans(Xtrain)
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
means <โ colMeans(Xtrain)
stdevs <โ sd(Xtrain)
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
means <โ colMeans(Xtrain)
stdevs <โ sd(Xtrain)
Xstrain <โ (Xtrain โ matrix(means,nrow(Xtrain),ncol(Xtrain),byrow=TRUE)) / matrix(stdevs ,nrow(Xtrain),ncol(Xtrain ), byrow=TRUE)
Xstest <โ (Xtest โ matrix(means,nrow(Xtest),ncol(Xtest),byrow=TRUE)) / matrix(stdevs ,nrow(Xtest),ncol(Xtest ), byrow=TRUE)
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
Must keep track of means and stdevs from training
data. Can do as variables returned from standardize
function:
standardize <โ function(X,means=apply(X,2,mean),stdevs=apply(X,2,sd), returnParms=FALSE) {
stdevs [ stdevs==0] <โ 1 N <โ nrow(X) p <โ ncol(X) X <โ (X โ matrix(rep(means,N),N,p,byrow=TRUE))/ matrix(rep( stdevs ,N),N,p,byrow=TRUE) if (returnParms) list (data=X,means=means,stdevs=stdevs) else X }
used like
tp <โ standardize(Xtrain,returnParms=TRUE) Xstrain <โ tp$data Xstest <โ standardize(Xtest, tp$means, tp$stdevs)
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
return( list ( weights=w, standardize=standardize ) )
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
return( list ( weights=w, standardize=standardize ) )
model <โ makeLLS(Xtrain,Ttrain,lambda) predictions <โ useLLS(model,Xtest)
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
return( list ( weights=w, standardize=standardize ) )
model <โ makeLLS(Xtrain,Ttrain,lambda) predictions <โ useLLS(model,Xtest)
Xs <โ model$standardize(X) predictions <โ Xs %*% model$weights
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
for ( trainf in c (0.2, 0.4, 0.6, 0.8, 0.9)) { for ( repi in 1:200) { for (lambda in seq (0,10, by=0.5)) {
} } }
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
for ( trainf in c (0.2, 0.4, 0.6, 0.8, 0.9)) { for ( repi in 1:200) { for (lambda in seq (0,10, by=0.5)) {
} } }
results <โ rbind(results, c( trainf ,lambda, trainRMSE, testRMSE))
results <โ c()
Modeling
Chuck Anderson
R Tips for Linear Modeling
Backing up Files in Unix
Collinearity
Now, the matrix has many rows (200) for each pair of
(trainf, lambda) values. How can we calculate the
means of those 200 values? Check out ?unique.
results [,1] [,2] [,3] [,4] [1,] 0.2 0.1 3.2 3. [2,] 0.2 0.5 5.3 3. [3,] 0.2 0.1 5.5 3. unique(results [,1:2]) [,1] [,2] [1,] 0.2 0. [2,] 0.2 0.