You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

长格式数据下的增长率计算咨询:松鼠个体体重增长率求解

Got it, let's break this down for you—whether you're working with your current wide-format squirrel data or switch to long-format, calculating weight growth rates is totally manageable with some tidy data tools. Here's how to do both:

处理宽格式数据的体重增长率计算

Your current data has one row per squirrel, with repeated columns for dates, ages, and weights (like date1/age1/wt1, date2/age2/wt2, etc.). First, we need to pair up each valid age-weight observation for a squirrel, then compute growth rates.

We'll use R's tidyverse package (standard for ecological data wrangling) to handle this:

# Load required package
library(tidyverse)

# Assume your data frame is named squirrel_wide
growth_wide <- squirrel_wide %>%
  # Reshape wide columns to pair age/wt for each time point
  pivot_longer(
    cols = starts_with(c("age", "wt")),
    names_to = c(".value", "time_point"),
    names_pattern = "(age|wt)(\\d+)"
  ) %>%
  # Remove rows where age or weight is missing (NA)
  drop_na(age, wt) %>%
  # Group by individual squirrel to calculate per-squirrel rates
  group_by(squirrel) %>%
  # Sort observations by age to ensure we're using chronological order
  arrange(age, .by_group = TRUE) %>%
  # Calculate two common growth metrics:
  # 1. Percent growth rate between consecutive measurements
  # 2. Daily weight gain (grams per day)
  mutate(
    percent_growth_rate = ((wt - lag(wt)) / lag(wt)) * 100,
    daily_weight_gain = (wt - lag(wt)) / (age - lag(age))
  ) %>%
  # Optional: Calculate overall average daily growth using linear regression
  # (this gives a single rate per squirrel, even with multiple time points)
  mutate(overall_daily_growth = coef(lm(wt ~ age, data = cur_data()))[2]) %>%
  ungroup()

What this does:

  • pivot_longer takes your wide columns and turns them into rows where each row is one age-weight measurement for a squirrel.
  • drop_na gets rid of useless rows where we don't have both age and weight data.
  • arrange ensures we're calculating growth in the correct order (from youngest to oldest).
  • The mutate steps compute:
    • Percent growth rate: How much weight the squirrel gained (as a percentage) between two measurements.
    • Daily weight gain: The average grams gained per day between two time points.
    • Overall daily growth: The slope of a linear regression of weight vs. age, which gives a single average growth rate per squirrel across all their measurements.
长格式数据的体重增长率计算

If your data was already in long-format (one row per squirrel per measurement, with columns like squirrel, age, wt, date, etc.), the process is even simpler—you skip the reshaping step entirely.

Example long-format data structure:

squirrelfieldBirthDatedateagewtlitterid
229222017-05-132017-05-141127684
229222017-05-132017-06-072552.97684
229762017-05-132017-05-16315.57692

Here's the code to compute growth rates for long-format data:

# Assume your long-format data frame is named squirrel_long
growth_long <- squirrel_long %>%
  drop_na(age, wt) %>%
  group_by(squirrel) %>%
  arrange(age, .by_group = TRUE) %>%
  mutate(
    percent_growth_rate = ((wt - lag(wt)) / lag(wt)) * 100,
    daily_weight_gain = (wt - lag(wt)) / (age - lag(age)),
    overall_daily_growth = coef(lm(wt ~ age, data = cur_data()))[2]
  ) %>%
  ungroup()

Extra tips:

  • If you're expecting exponential growth (common in young animals), use logarithmic growth rate instead: log(wt) - log(lag(wt)) (this gives the continuous growth rate).
  • Squirrels with only one measurement will have NA for the consecutive growth rates—you can filter these out with filter(!is.na(percent_growth_rate)) if needed.
  • If weight growth isn't linear (e.g., slows down as the squirrel matures), consider fitting a nonlinear model (like a logistic growth curve) instead of linear regression for the overall rate.

内容的提问来源于stack exchange,提问作者Blundering Ecologist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:34:11