{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "4fc0e5e3", "metadata": {}, "outputs": [], "source": [ "import matplotlib\n", "import matplotlib.pyplot as plt\n", "matplotlib.rcParams['figure.figsize'] = (15, 8)\n", "import numpy as np\n", "import pandas as pd\n", "\n", "rng = np.random.default_rng()" ] }, { "cell_type": "markdown", "id": "08e983c8", "metadata": {}, "source": [ "Today we're going to look at real data: how newborn (i.e., \"neonate\") body mass\n", "is related to adult body mass,\n", "across a bunch of mammal species\n", "(in the order Carnivora, including dogs, cats, bears, weasels, and seals). [(data link)](https://github.com/UOdsci/dsci345/blob/main/class_material/slides/data/carnivora_sizes.csv)" ] }, { "cell_type": "code", "execution_count": 2, "id": "30e7adf5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
FamilyNeonateBodyMass_gAdultBodyMass_gAgeatEyeOpening_dWeaningAge_d
Binomial
Canis aureusCanidae211.829658.707.5061.30
Canis latransCanidae200.0111989.1011.9443.71
Canis lupusCanidae412.3131756.5114.0144.82
Canis mesomelasCanidae177.208247.30NaN34.10
Callorhinus ursinusOtariidae5354.8055464.820.00108.69
..................
Vulpes lagopusCanidae69.173584.3715.0349.50
Vulpes veloxCanidae39.942088.0012.5047.08
Vulpes vulpesCanidae100.494820.3614.0150.71
Vulpes zerdaCanidae28.041317.1316.9565.56
Zalophus californianusOtariidae6347.89137194.860.00319.01
\n", "

145 rows × 5 columns

\n", "
" ], "text/plain": [ " Family NeonateBodyMass_g AdultBodyMass_g \\\n", "Binomial \n", "Canis aureus Canidae 211.82 9658.70 \n", "Canis latrans Canidae 200.01 11989.10 \n", "Canis lupus Canidae 412.31 31756.51 \n", "Canis mesomelas Canidae 177.20 8247.30 \n", "Callorhinus ursinus Otariidae 5354.80 55464.82 \n", "... ... ... ... \n", "Vulpes lagopus Canidae 69.17 3584.37 \n", "Vulpes velox Canidae 39.94 2088.00 \n", "Vulpes vulpes Canidae 100.49 4820.36 \n", "Vulpes zerda Canidae 28.04 1317.13 \n", "Zalophus californianus Otariidae 6347.89 137194.86 \n", "\n", " AgeatEyeOpening_d WeaningAge_d \n", "Binomial \n", "Canis aureus 7.50 61.30 \n", "Canis latrans 11.94 43.71 \n", "Canis lupus 14.01 44.82 \n", "Canis mesomelas NaN 34.10 \n", "Callorhinus ursinus 0.00 108.69 \n", "... ... ... \n", "Vulpes lagopus 15.03 49.50 \n", "Vulpes velox 12.50 47.08 \n", "Vulpes vulpes 14.01 50.71 \n", "Vulpes zerda 16.95 65.56 \n", "Zalophus californianus 0.00 319.01 \n", "\n", "[145 rows x 5 columns]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "carnivores = pd.read_csv(\"data/carnivora_sizes.csv\").set_index(\"Binomial\")\n", "carnivores" ] }, { "cell_type": "markdown", "id": "7998277e", "metadata": {}, "source": [ "This is a pandas data frame - you might want to refer to the [pandas cheat sheet](https://pandas.pydata.org/Pandas_Cheat_Sheet.pdf)." ] }, { "cell_type": "markdown", "id": "d662273c", "metadata": {}, "source": [ "**1.** Make a new data frame, `weasels`, that is just those rows with `Family == \"Mustelidae\"`." ] }, { "cell_type": "code", "execution_count": null, "id": "7d22cd77", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "4313b46e", "metadata": {}, "source": [ "**2.** Make a (scatter) plot of neonate body mass against adult body mass for the weasels." ] }, { "cell_type": "code", "execution_count": null, "id": "1fa18d93", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "5b9451fb", "metadata": {}, "source": [ "**3.** Make new variables for $\\log_{10}$ neonate and adult body mass, and plot these against each other.\n", "Which plot looks better? " ] }, { "cell_type": "code", "execution_count": null, "id": "ccd071b1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "97c243d9", "metadata": {}, "source": [ "**4.** Fit a standard linear model to *(a)* neonate and adult body mass, and *(b)* log10 neonate and log10 adult body mass. (Use 'neonate' as the response variable and 'adult' as the predictor.)" ] }, { "cell_type": "code", "execution_count": null, "id": "e3df7ce9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "5451bb38", "metadata": {}, "source": [ "**5.** In the last step, you've obtained two \"best-fit\" lines:\n", "if $N_i$ is the neonate body mass of species $i$ and $A_i$ is the adult body mass of the same species,\n", "you have *(a)*\n", "$$ N_i = m A_i + b + \\epsilon_i, $$\n", "and so the estimator of neonate body mass ($n$) as a function of adult mass ($a$) is\n", "$$ \\hat n = m a + b, $$\n", "and *(b)*\n", "$$ \\log_{10}(N_i) = m_\\text{log} \\log_{10}(A_i) + b_\\text{log} + \\epsilon_i, $$\n", "and so a different estimator is\n", "$$ \\tilde{n} = a^{m_\\text{log}} 10^{b_\\text{log}} . $$\n", "\n", "Plot *both* lines on *both* scatter plots above." ] }, { "cell_type": "code", "execution_count": null, "id": "adcfe490", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3964cb5d", "metadata": {}, "source": [ "**6.** Suppose someone discoveres a new species of mustellid,\n", "and estimates their adults weigh about 200g.\n", "How much do you predict their neonates weigh?" ] }, { "cell_type": "code", "execution_count": null, "id": "1b76c01a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.8" } }, "nbformat": 4, "nbformat_minor": 5 }