Skip to content

Commit

Permalink
adding stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
quantifish committed Sep 26, 2023
1 parent f477cea commit ed0e7e7
Show file tree
Hide file tree
Showing 32 changed files with 375 additions and 100 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(Influence)
export(coeffs)
export(geo_mean)
export(get_bayes_R2)
export(get_coefs)
Expand All @@ -25,11 +27,13 @@ export(plot_influ)
export(plot_predicted_residuals)
export(plot_qq)
export(plot_step)
export(ses)
export(table_criterion)
import(brms)
import(dplyr)
import(ggplot2)
import(patchwork)
import(proto)
importFrom(brms,add_criterion)
importFrom(brms,posterior_samples)
importFrom(gtable,gtable_filter)
Expand Down
7 changes: 7 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' Simulated catch of lobsters per pot
#'
#' Simulated catch of lobsters per pot.
#'
#' @format A data.frame with four fields:
#'
"lobsters_per_pot"
3 changes: 3 additions & 0 deletions R/get-coefs.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ get_coefs_raw <- function(fit, var = "area") {
if (!is.brmsfit(fit)) stop("fit is not an object of class brmsfit.")

ps <- as_draws_df(x = fit, variable = var, regex = TRUE) %>%
as.data.frame() %>%
mutate(iteration = .data$.draw) %>%
select(-.data$.iteration, -.data$.chain, -.data$.draw) %>%
melt(id.vars = "iteration") %>%
Expand Down Expand Up @@ -148,6 +149,7 @@ get_coefs <- function(fit, var = "area",
# data.frame() %>%
# mutate(variable = rownames(.))
ps <- as_draws_df(x = fit, variable = paste0("r_", var), regex = TRUE) %>%
as.data.frame() %>%
mutate(iteration = .data$.draw) %>%
select(-.data$.iteration, -.data$.chain, -.data$.draw) %>%
melt(id.vars = "iteration") %>%
Expand All @@ -169,6 +171,7 @@ get_coefs <- function(fit, var = "area",
# names(e2) <- names(eff)
# eff <- rbind(e2, eff)
ps <- as_draws_df(x = fit, variable = var, regex = TRUE) %>%
as.data.frame() %>%
mutate(iteration = .data$.draw) %>%
select(-.data$.iteration, -.data$.chain, -.data$.draw) %>%
melt(id.vars = "iteration") %>%
Expand Down
21 changes: 18 additions & 3 deletions tests/testthat/influ.R → R/influ.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@
#' @export Influence
NULL

library(proto)

#' The influence prototype object.
#'
#' @seealso Influence$new
#' @import proto
#' @export
#'
Influence = proto()

#' Create a new Influence object.
Expand All @@ -57,6 +58,7 @@ Influence = proto()
#' @param response The response term in the model
#' @param focus The focus term for which influence is to be calculated.
#' @return A new Influence object
#'
Influence$new <- function(.,model,data=NULL,response=NULL,focus=NULL){
instance = .$proto(
model = model,
Expand All @@ -74,6 +76,7 @@ Influence$new <- function(.,model,data=NULL,response=NULL,focus=NULL){
#'
#' @name Influence$init
#' @return None
#'
Influence$init <- function(.){

# If no data was supplied....
Expand Down Expand Up @@ -113,7 +116,9 @@ Influence$init <- function(.){
#' @name coeffs
#' @param model The model to extract coefficients from
#' @param term The term in the model for which coefficients are extracted
#' @return A vector of coefficients
#' @return A vector of coefficients
#' @export
#'
coeffs = function(.,model=.$model,term=.$focus){
coeffs = coefficients(model)
rows = substr(names(coeffs),1,nchar(term))==term
Expand All @@ -127,6 +132,8 @@ coeffs = function(.,model=.$model,term=.$focus){
#' @name ses
#' @param model The model to extract standard errors for a coefficient from
#' @param term The term in the model for which coefficients SEs are extracted
#' @export
#'
ses = function(.,model=.$model,term=.$focus){
type = class(model)[1]
if(type=='glm'|type=='negbin'){
Expand Down Expand Up @@ -156,6 +163,7 @@ ses = function(.,model=.$model,term=.$focus){
#' @param model The model to extract effects from
#' @param term The term in the model for which effects are extracted
#' @return A numeric vector of effects for the model term
#'
Influence$effects = function(.,model=.$model,term=.$focus){
coeffs = .$coeffs(model,term)
exp(coeffs-mean(coeffs))
Expand All @@ -167,6 +175,7 @@ Influence$effects = function(.,model=.$model,term=.$focus){
#'
#' @name Influence$calc
#' @return None
#'
Influence$calc <- function(.){
#Get observed values
observed = .$model$model[,.$response]
Expand Down Expand Up @@ -293,6 +302,7 @@ Influence$calc <- function(.){
#'
#' @name Influence$stanPlot
#' @return None
#'
Influence$stanPlot <- function(.){
with(.$indices,{
plot(NA,ylim=c(0,max(unstan,stanUpper)),xlim=c(1,length(level)),las=1,ylab='Index',xlab=.$labels[[.$focus]],xaxt='n')
Expand All @@ -313,6 +323,7 @@ Influence$stanPlot <- function(.){
#' panel with the previous steps shown as a dashed line and other steps shown as dashed grey lines. If FALSE then a single panel
#' plot is produced with symbols for each step
#' @return None
#'
Influence$stepPlot <- function(.,panels=T,setpar=T){
startCol = 6
cols = startCol:ncol(.$indices)
Expand Down Expand Up @@ -350,6 +361,7 @@ Influence$stepPlot <- function(.,panels=T,setpar=T){
#' @name Influence$influPlot
#' @param panels Whether or not to produce a plot with separate panels for each variable
#' @return None
#'
Influence$influPlot <- function(.,panels=T,setpar=T){
cols = 2:ncol(.$influences)
ylim=exp(range(.$influences[,cols]))
Expand Down Expand Up @@ -381,6 +393,7 @@ Influence$influPlot <- function(.,panels=T,setpar=T){
#'
#' @name Influence$stepAndInfluPlot
#' @return None
#'
Influence$stepAndInfluPlot <- function(.){
par(mfcol=c(ncol(.$indices)-5,2),mar=c(0,5,0,0),oma=c(4,1,1,1))
.$stepPlot(setpar=F)
Expand All @@ -397,6 +410,7 @@ Influence$stepAndInfluPlot <- function(.){
#' In most cases this can be deduced from the term, but for complexes terms this argumey may also need to be supplied (an error message will tell you if it does).
#' @return None
#' @seealso cdiPlotAll
#'
Influence$cdiPlot <- function(.,term,variable=NULL){
par(oma=c(1,1,1,1),cex.lab=1.25,cex.axis=1.25)
layout(matrix(c(1,1,0,2,2,3,2,2,3),3,3,byrow=TRUE))
Expand Down Expand Up @@ -496,6 +510,7 @@ Influence$cdiPlot <- function(.,term,variable=NULL){
#' @name Influence$cdiPlotAll
#' @param done A function that is called (with the term string as an argument) after each CDI plot is generated. Optional.
#' @return None
#'
Influence$cdiPlotAll <- function(.,done=function(term){
cat("cdiPlot for",term,". Press enter for next")
scan()
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# influ2 <img src="man/figures/logo.png" align="right" height=140/>

[![Build Status](https://travis-ci.org/quantifish/influ2.svg?branch=master)](https://travis-ci.com/quantifish/influ2)

Nokome Bentley's R package `influ` was developed for use with frequentist models fitted in R using the `glm` function. The `infu2` package is the Bayesian couterpart and has been developed for use with `brms`. It works with population-level or group-level effects, the Bayesian equivalents of fixed-effects and random-effects. It contains functions for extracting coefficients, calculating the influence of terms, generating CDI plots, step plots, and other diagnostic plots.
Nokome Bentley's R package `influ` was developed for use with frequentist models
fitted in R using the `glm` function. The `infu2` package is the Bayesian
couterpart and has been developed for use with `brms`. It works with
population-level or group-level effects, the Bayesian equivalents of
fixed-effects and random-effects. It contains functions for extracting
coefficients, calculating the influence of terms, generating CDI plots, step
plots, and other diagnostic plots.

## Installation

Expand Down
Binary file added data/lobsters_per_pot.rda
Binary file not shown.
11 changes: 11 additions & 0 deletions man/Influence-cash-calc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/Influence-cash-cdiPlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/Influence-cash-cdiPlotAll.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions man/Influence-cash-effects.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/Influence-cash-influPlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions man/Influence-cash-init.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions man/Influence-cash-new.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions man/Influence-cash-stanPlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions man/Influence-cash-stepAndInfluPlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions man/Influence-cash-stepPlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/Influence.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/coeffs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ed0e7e7

Please sign in to comment.