Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • c867f21
  • /
  • Cross validating codes
  • /
  • 01 - refit_models_unstd.Rmd
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:4f486573238e8971a1b17a2dc8a8b6ba3348ab76
directory badge
swh:1:dir:f52c58daf971b90f0ea8392f9f530b88abe7d5d1

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
01 - refit_models_unstd.Rmd
---
title: "Script to refit models and extract coefficients for unstandardised data"
author: "Rachel Mawer"
date: "2023-11-29"
output: 
  html_document:
    toc: true
    toc_float: true
    theme: cerulean
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F,message=F)

#main packages #delete/add as needed
library(dplyr)
library(ggplot2)
library(knitr)
library(ggpubr)
library(plotly)
library(pracma) #for standard error
library(reactable)
library(stringr)
library(amt)
```

## About

In this file, all final models for all fish and states are read in and refitted with the unstandardised data - e.g. model structure obtained is reused.

Resulting coefficients are saved as a file for use in simulations.

```{r read in file names etc}

fish_ids <- list.files("ssf modelling - file per fish")

files <- list.files("ssf modelling - file per fish",full.names = T)
files <- files[!grepl("coeffs",files)] #remove the non fish folder

#og data files
data_files <- list.files("data/final ssf + hmm df",full.names = T)

save_loc <- "unstandardised ssf model outputs"

```

```{r run the loop}
#loop to do everything

i <- 1
z <- 1

for(i in 1:length(files)){
  fish_id <- fish_ids[i]
  fish_files <- list.files(files[i],full.names = T)
  fish_files <- fish_files[grepl("final",fish_files)]
  
  fish_data_files <-data_files[grepl(fish_id,data_files)]
  
  for(z in 1:length(fish_files)){
    model <- readRDS(fish_files[z]) #get model
    data <- read.csv(fish_data_files[z]) #get data
    
    state <- data$state %>% unique()
    #extract model formula
    frm_part <- as.character(model$formula)[3] %>% str_remove_all("_strdised") %>% str_remove_all("\n")
    frm <- paste("case_ ~",frm_part) %>% as.formula()

    new_mod <- fit_issf(formula = frm,data=data)
    coefs <- summary(new_mod)$coefficients %>% as.data.frame() %>% tibble::rownames_to_column("params")
    coefs$fish_id <- fish_id
    coefs$state <- state
    coefs$species <- unique(data$species)
    
    if(z==1){
      fish_coefs <- coefs
    } else{
      fish_coefs <- bind_rows(fish_coefs,coefs)
    }
  }
  if(i==1){
    all_coefs <- fish_coefs
  }else{
    all_coefs <- bind_rows(all_coefs,fish_coefs)
  }
}

write.csv(all_coefs,"FINAL RESULTS/all_coefs_both_states_UNSTANDARDISED.csv",row.names=F)
```

```{r data loc pooled}

fish_ids <- list.files("ssf modelling - file per fish - pooled")

files <- list.files("ssf modelling - file per fish - pooled",full.names = T)
files <- files[!grepl("coeffs",files)] #remove the non fish folder

#og data files
data_files <- list.files("data/SSF dataframes",full.names = T)

```

```{r and for the both states data}
#loop to do everything

i <- 1
z <- 1

for(i in 1:length(files)){
  fish_id <- fish_ids[i]
  fish_files <- list.files(files[i],full.names = T)
  fish_files <- fish_files[grepl("final",fish_files)]
  
  fish_data_files <-data_files[grepl(fish_id,data_files)]

    model <- readRDS(fish_files) #get model

    data <- read.csv(fish_data_files) #get data
    
    #extract model formula
    frm_part <- as.character(model$formula)[3] %>% str_remove_all("_strdised") %>% str_remove_all("\n")
    frm <- paste("case_ ~",frm_part) %>% as.formula()
    #need to save name
    
    new_mod <- fit_issf(formula = frm,data=data)
    coefs <- summary(new_mod)$coefficients %>% as.data.frame() %>% tibble::rownames_to_column("params")
    coefs$fish_id <- fish_id
    coefs$state <- "pooled"
    coefs$species <- unique(data$species)
    
    fish_coefs <- coefs
    
  if(i==1){
    all_coefs <- fish_coefs
  }else{
    all_coefs <- bind_rows(all_coefs,fish_coefs)
  }
}


write.csv(all_coefs,"FINAL RESULTS/all_coefs_pooled_UNSTANDARDISED.csv",row.names=F)
```

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API