📊 Week 7: Functions and T-Tests

Master Custom Functions and Statistical Comparisons 🎯

Welcome to Week 7! This week, we explore custom functions in R and two-sample t-tests - essential skills for creating reusable code and comparing groups in agricultural research. Learn to write functions, perform statistical tests, and interpret results!

🎯 What You'll Learn This Week

🔧 Custom Functions - Create reusable functions for repetitive tasks
📊 Sample Size Functions - Build functions for statistical calculations
🔬 Hypothesis Formation - Set up null and alternative hypotheses
📈 Two-Sample T-Tests - Compare means between two groups
⚖️ Welch's T-Test - Handle unequal variances between groups
🎯 One-Sided Tests - Test directional hypotheses

🚀 Getting Started: Step-by-Step Guide

Step 1: Launch Week 7 Binder Environment 🌐

Click the "Launch Week 7" button above to start your R environment. This will take 2-5 minutes to load with all necessary packages for functions and t-tests.

Step 2: Navigate to Class Activity 📚

Once Binder loads, you'll see the Jupyter Notebook interface. In the left panel, you'll see:

Click on the class_activity folder to access this week's content.

Step 3: Open the Week 7 Lab Notebook 📖

Inside the class_activity folder, double-click on Week7_Regression_Analysis.ipynb to open the interactive lab notebook.

Step 4: Explore Functions and T-Tests 📊

This week we'll work with custom functions and the iris dataset! The notebook will guide you through:

🎯 Interactive Learning Tools

Practice with Functions and T-Tests

Use these interactive tools to understand function creation and t-test concepts before working with R code:

💡 Tip: Use these tools to practice function syntax and visualize t-test concepts before applying them in your R notebook!

🧮 Key R Functions This Week

Function Creation

myFunction <- function(arg1, arg2) { # Define function
result <- arg1 + arg2 # Function body
return(result) # Return value
} # End function

Sample Size Function

calculate_sample_size <- function(prev, alpha, margin_error) {
z <- qnorm(1 - alpha / 2) # Critical z-value
n <- z^2 * prev * (1 - prev) / (margin_error^2)
return(ceiling(n)) # Round up
}

Two-Sample T-Tests

t.test(group1, group2, alternative = "two.sided") # Two-sided test
t.test(group1, group2, var.equal = FALSE) # Welch's t-test
t.test(group1, group2, alternative = "greater") # One-sided test
data %>% filter(Species == "setosa") # Data filtering

📝 Assignment 7: Fertilizer Treatment Comparison

Step 1: Access Assignment Folder 📋

From the main directory, click on the assignment folder to access Assignment 7.

Step 2: Open Assignment 7 Notebook 📄

Double-click on Assignment7.ipynb to open your assignment on t-tests for agricultural data.

Assignment Overview (20 points total)

📊

Part 1: Data Loading & Visualization (4 points)

Load wheat yield data and create visualizations

🔬

Part 2: Hypothesis Formation (3 points)

Set up null and alternative hypotheses

📈

Part 3: T-Test Analysis (5 points)

Perform two-sample t-test and interpret results

🎯

Part 4: Results Interpretation (4 points)

Analyze p-values and draw conclusions

Part 5: One-Sided Test (4 points)

Test if nitrogen fertilizer yields are higher

Step 3: Analyze Fertilizer Treatment Data 🌾

The assignment focuses on agricultural research applications:

Learn to make evidence-based agricultural decisions using statistical testing!

🌾 Why This Matters in Agriculture

🌱 Treatment Efficacy - Test if fertilizers significantly improve crop yields
🌾 Variety Comparisons - Compare performance between crop varieties
🔬 Experimental Design - Create reusable functions for statistical calculations
🐛 Pest Management - Compare effectiveness of different control methods
💧 Irrigation Studies - Test water management strategies

Function and T-Test Skills Help You:

💾 Saving Your Work

⚠️ Important: Binder environments are temporary! Always save your work locally.

Download Your Notebook 📥

When you're done working, save your progress:

  1. Save your notebook: File → Save
  2. Download .ipynb file: File → Download
  3. Export HTML/PDF: File → Save and Export Notebook As → HTML

Continue Your Progress Later 🔄

To resume your work:

  1. Launch Binder again
  2. Click Upload button
  3. Upload your saved .ipynb file
  4. Continue where you left off!

📤 Submission Requirements

For Assignment 7, submit TWO files to UC Davis Canvas:

📄

HTML/PDF Report

Your completed assignment with all outputs and analysis

💾

.ipynb File

Your notebook code as backup

Due Date: Check Canvas for assignment deadline

🎯 Learning Objectives

By the end of this week, you will be able to:

Create custom functions with parameters and return values
Build reusable functions for statistical calculations
Form null and alternative hypotheses for group comparisons
Perform two-sample t-tests using Welch's method
Interpret t-statistics, p-values, and confidence intervals
Apply statistical testing to agricultural research problems

❓ Need Help?

📧 Contact Information

Mohammadreza Narimani
📧 mnarimani@ucdavis.edu
🏫 Department of Biological and Agricultural Engineering, UC Davis

🔧 Common Issues

📚 Additional Resources

🌟 Tips for Success

💡 Best Practices

⚡ Keyboard Shortcuts

Shift + Enter Run current cell and move to next
Ctrl + Enter Run current cell and stay in place
Tab Auto-complete function names
?function Get help for any R function

🎉 Ready to Start?

Click the Binder badge below to launch Week 7!

Happy function building and testing! 🔧📊