Skip to content
Snippets Groups Projects
Commit 418351a4 authored by Dhanush Kumar Reddy Narayana Reddy's avatar Dhanush Kumar Reddy Narayana Reddy
Browse files

first commit

parent 88a2af9b
No related branches found
No related tags found
No related merge requests found
^.*\.Rproj$
^\.Rproj\.user$
.Rproj.user
.Rhistory
.RData
.Ruserdata
Package: group5labbonus
Type: Package
Title: What the Package Does (Short Line)
Version: 1.0
Date: 2024-11-03
Author: Who wrote it
Maintainer: Who to complain to <yourfault@somewhere.net>
Description: More about what it does (maybe more than one line).
License: GPL-3 + file LICENSE
RoxygenNote: 7.3.2
Encoding: UTF-8
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
LICENSE 0 → 100644
This diff is collapsed.
# Generated by roxygen2: do not edit by hand
export(ridgereg)
#' Ridge Regression Reference Class
#'
#' This class implements ridge regression with methods for initialization, coefficient extraction,
#' prediction, and printing the model summary.
#'
#' @field formula_string A character string representing the regression formula.
#' @field data_string A character string representing the data frame used.
#' @field formula A formula specifying the relationship between dependent and independent variables.
#' @field data A data frame containing the variables specified in the formula.
#' @field lambda A numeric value for the ridge penalty parameter.
#' @field coefficients A matrix containing the estimated ridge regression coefficients.
#' @field fitted_values A matrix of the fitted (predicted) values.
#' @field residuals A matrix of the residuals (difference between actual and fitted values).
#'
#' @param formula A formula specifying the model.
#' @param data A data frame containing the variables in the formula.
#' @param lambda The ridge penalty parameter.
#'
#' @export ridgereg
ridgereg <- setRefClass(
"ridgereg",
fields = list(
formula_string = "character",
data_string = "character",
formula = "formula",
data = "data.frame",
lambda = "numeric",
coefficients = "matrix",
fitted_values = "matrix",
residuals = "matrix"
),
methods = list(
initialize = function(formula, data, lambda) {
formula_string <<- deparse(formula)
data_string <<- deparse(substitute(data))
lambda <<- lambda
X <- model.matrix(formula, data)
y <- data[[all.vars(formula)[1]]]
X_norm <- scale(X[, -1])
X_norm <- cbind(1, X_norm)
I <- diag(ncol(X_norm))
I[1, 1] <- 0
beta_ridge <- solve(t(X_norm) %*% X_norm + lambda * I) %*% t(X_norm) %*% y
coefficients <<- beta_ridge
fitted_values <<- X_norm %*% beta_ridge
residuals <<- y - fitted_values
},
print = function() {
class_name <- class(.self)[1]
cat("Call:\n")
cat(class_name,"(formula = ",formula_string ,", data = ",data_string,",lambda = ",lambda,")","\n", sep = "")
cat("\nCoefficients:\n")
coef_names <- rownames(coefficients)
coef_values <- as.vector(coefficients)
names(coef_values) <- coef_names
print.default(coef_values)
},
pred = function() {
return(as.vector(fitted_values))
},
coef = function() {
named_coefs <- setNames(as.vector(coefficients), rownames(coefficients))
return(named_coefs)
}
)
)
# model <- ridgereg$new(Sepal.Length ~ Sepal.Width + Petal.Length, data = iris, lambda = 0.1)
# model$print()
# model$pred()
# model$coef()
* Edit the help file skeletons in 'man', possibly combining help files for
multiple functions.
* Edit the package 'DESCRIPTION'.
* Edit the exports in 'NAMESPACE', and add necessary imports.
* Put any C/C++/Fortran code in 'src'.
* If you have compiled code, add a useDynLib() directive to 'NAMESPACE'.
* Run R CMD build to build the package tarball.
* Run R CMD check to check the package tarball.
Read "Writing R Extensions" for more information.
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace,vignette
\name{fun}
\alias{fun}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{
A Capitalized Title (ideally limited to 65 characters)
}
\description{
%% ~~ A concise (1-5 lines) description of what the function does. ~~
}
\usage{
fun()
}
%- maybe also 'usage' for other objects documented here.
\details{
%% ~~ If necessary, more details than the description above ~~
}
\value{
%% ~Describe the value returned
%% If it is a LIST, use
%% \item{comp1 }{Description of 'comp1'}
%% \item{comp2 }{Description of 'comp2'}
%% ...
}
\references{
%% ~put references to the literature/web site here ~
}
\author{
%% ~~who you are~~
}
\note{
%% ~~further notes~~
}
%% ~Make other sections like Warning with \section{Warning }{....} ~
\seealso{
%% ~~objects to See Also as \code{\link{help}}, ~~~
}
\examples{
##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or standard data sets, see data().
## The function is currently defined as
function ()
print()
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory (show via RShowDoc("KEYWORDS")):
% \keyword{ ~kwd1 }
% \keyword{ ~kwd2 }
% Use only one keyword per line.
% For non-standard keywords, use \concept instead of \keyword:
% \concept{ ~cpt1 }
% \concept{ ~cpt2 }
% Use only one concept per line.
\name{group5labbonus-package}
\alias{group5labbonus-package}
\alias{group5labbonus}
\docType{package}
\title{
\packageTitle{group5labbonus}
}
\description{
\packageDescription{group5labbonus}
}
\details{
%% ~~ An overview of how to use the package, ~~
%% ~~ including the most important functions ~~
}
\author{
\packageAuthor{group5labbonus}
Maintainer: \packageMaintainer{group5labbonus}
}
\references{
%% ~~ Literature or other references for background information ~~
}
\keyword{package}
%% Uncomment below to imitate parts of library(help = group5labbonus)
%\section{The \file{DESCRIPTION} File}{\packageDESCRIPTION{group5labbonus}}
%\section{Documentation Index}{\packageIndices{group5labbonus}}
\seealso{
%% ~~ Optional links to other man pages, e.g. ~~
%% ~~ \code{\link[<pkg>:<pkg>-package]{<pkg>}} ~~
}
\examples{
%% ~~ Optional simple examples of the most important functions ~~
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ridgereg.R
\docType{class}
\name{ridgereg-class}
\alias{ridgereg-class}
\alias{ridgereg}
\title{Ridge Regression Reference Class}
\arguments{
\item{formula}{A formula specifying the model.}
\item{data}{A data frame containing the variables in the formula.}
\item{lambda}{The ridge penalty parameter.}
}
\description{
This class implements ridge regression with methods for initialization, coefficient extraction,
prediction, and printing the model summary.
}
\section{Fields}{
\describe{
\item{\code{formula_string}}{A character string representing the regression formula.}
\item{\code{data_string}}{A character string representing the data frame used.}
\item{\code{formula}}{A formula specifying the relationship between dependent and independent variables.}
\item{\code{data}}{A data frame containing the variables specified in the formula.}
\item{\code{lambda}}{A numeric value for the ridge penalty parameter.}
\item{\code{coefficients}}{A matrix containing the estimated ridge regression coefficients.}
\item{\code{fitted_values}}{A matrix of the fitted (predicted) values.}
\item{\code{residuals}}{A matrix of the residuals (difference between actual and fitted values).}
}}
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html
library(testthat)
library(group5labbonus)
test_check("group5labbonus")
library(testthat)
test_that("ridgereg initializes correctly", {
model <- ridgereg$new(Sepal.Length ~ Sepal.Width + Petal.Length, data = iris, lambda = 0.1)
# Check if coefficients are initialized correctly
expect_true(is.matrix(model$coefficients))
expect_equal(ncol(model$coefficients), 1)
# Check if fitted values and residuals are matrices
expect_true(is.matrix(model$fitted_values))
expect_true(is.matrix(model$residuals))
})
test_that("ridgereg predicts correctly", {
model <- ridgereg$new(Sepal.Length ~ Sepal.Width + Petal.Length, data = iris, lambda = 0.1)
# Test if predictions return a numeric vector
preds <- model$pred()
expect_true(is.numeric(preds))
expect_equal(length(preds), nrow(iris))
})
test_that("ridgereg coefficients extraction works", {
model <- ridgereg$new(Sepal.Length ~ Sepal.Width + Petal.Length, data = iris, lambda = 0.1)
# Test if coefficients are returned as a named numeric vector
coefs <- model$coef()
expect_true(is.numeric(coefs))
expect_true(!is.null(names(coefs)))
})
test_that("ridgereg print method displays correctly", {
model <- ridgereg$new(Sepal.Length ~ Sepal.Width + Petal.Length, data = iris, lambda = 0.1)
# Capture the print output
output <- capture.output(model$print())
# Check if the output contains relevant information
expect_true(any(grepl("Call:", output)))
expect_true(any(grepl("Coefficients:", output)))
expect_true(any(grepl("Sepal.Width", output)))
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment