Podcast on Statistical Analysis with R

Statistical Analysis with R: A Student's Guide to Models & Data

Podcast

Analýza biologických dát v R0:00 / 26:45
0:001:00 zbývá
SaraNa konci nasledujúcich desiatich minút zistíte, prečo R funguje úplne inak, ako vás učili o štatistickom softvéri – a prečo je to vaša tajná zbraň pri analýze dát.
SamPresne tak, Sara. Sľubujem vám „aha“ moment, ktorý zmení váš pohľad na prácu s dátami. Zabudnite na nekonečné klikanie v menu. Toto je o niečom inom.
Chapters

Analýza biologických dát v R

Délka: 26 minut

Kapitoly

Úvod do R

Prvé kroky a základné operácie

Dátové rámce: Organizácia dát

Prieskumná analýza dát

The Two Parts of a Model

The Linear Model Family

Beyond the Basics with GLMs

Building the Best Model

Why Not Just T-Tests?

A Cockroach Experiment

Hypotheses and Variables

Interpreting the Results

Finding the Difference

Simplifying the Story

Tackling Collinearity with PCA

Building the 'Everything' Model

Scaling for a Fair Comparison

Finding the Final Model

The Final Frontier: Biostats

Understanding the Study Design

Grand Summary & Goodbye

Přepis

Sara: Na konci nasledujúcich desiatich minút zistíte, prečo R funguje úplne inak, ako vás učili o štatistickom softvéri – a prečo je to vaša tajná zbraň pri analýze dát.

Sam: Presne tak, Sara. Sľubujem vám „aha“ moment, ktorý zmení váš pohľad na prácu s dátami. Zabudnite na nekonečné klikanie v menu. Toto je o niečom inom.

Sara: To je odvážne tvrdenie! Poďme na to. Počúvate Studyfi Podcast a dnes sa ponoríme do programovacieho jazyka R pre analýzu biologických dát.

Sam: Super. Takže, čo je vlastne R? Predstavte si ho nie ako softvér, ale ako prostredie. Je to kombinácia extrémne výkonnej kalkulačky, grafického nástroja a programovacieho jazyka.

Sara: To znie… zložito. Prečo by som si mal vybrať práve R, keď existujú programy s jednoduchými tlačidlami a menu?

Sam: Skvelá otázka. Je pravda, že R nie je práve užívateľsky prívetivý na prvý pohľad. Nemá veľa „pull-down“ menu. Všetko je založené na príkazoch. Je to ako učiť sa šoférovať auto s manuálnou prevodovkou namiesto automatu.

Sara: Takže na začiatku je to ťažšie, ale potom máš plnú kontrolu nad vozidlom.

Sam: Presne! A to je obrovská výhoda. R vás núti premýšľať o tom, čo robíte. Nedá vám len kopu výstupov, ale presne to, o čo požiadate. A najlepšie na tom je, že je úplne zadarmo a má obrovskú komunitu, ktorá vytvára tisíce ďalších balíčkov.

Sara: Dobre, presvedčil si ma, aby som to aspoň skúsila. Ako začať? Predpokladám, že si ho musím najprv nainštalovať.

Sam: Áno, stačí ísť na webovú stránku r-project.com. Po inštalácii uvidíte príkazový riadok. A tu sa začína zábava. Môžete ho použiť ako bežnú kalkulačku na sčítanie, odčítanie, násobenie, delenie...

Sara: Takže môžem jednoducho napísať „2 plus 2“ a stlačiť enter?

Sam: Skoro. Napíšeš 2 + 2 a R ti vráti výsledok 4. Ale jeho skutočná sila je vo funkciách ako mean() pre priemer alebo sd() pre štandardnú odchýlku.

Sara: A musím si pamätať všetky tie názvy funkcií? Čo ak urobím preklep?

Sam: Och, R si na to potrpí. Je citlivý na veľké a malé písmená. mean s malým „m“ je niečo úplne iné ako Mean s veľkým „M“. Je ako ten prísny profesor, ktorý vám neuzná prácu, ak nemáte správne napísané meno.

Sara: Dobre, na to si dám pozor. Písať príkazy znie ako niečo z filmov o hakeroch.

Sam: Trochu áno! Ale keď si na to zvyknete, je to neuveriteľne rýchle. Ďalším kľúčovým konceptom sú dátové rámce, teda data frames. Toto je ten sľubovaný „aha“ moment.

Sara: Dátové rámce? To znie ako nejaká tabuľka v Exceli.

Sam: Presne tak! Predstavte si dátový rámec ako inteligentnú tabuľku. Každý stĺpec je premenná – napríklad pôda, vzdialenosť, množstvo – a každý riadok je jedno pozorovanie alebo meranie.

Sara: Takže si môžem načítať dáta priamo z Excelu?

Sam: Áno, je to veľmi jednoduché. Skopírujete tabuľku a použijete príkaz ako dat <- read.delim("clipboard"). A bum, vaše dáta sú v R, pripravené na analýzu. Je dôležité, aby názvy stĺpcov neobsahovali medzery a aby každý stĺpec mal rovnaký počet riadkov.

Sara: A čo chýbajúce dáta? Občas sa stane, že nejaké meranie chýba.

Sam: R s tým počíta. Chýbajúce hodnoty sa označujú ako NA, čo znamená „Not Available“. A existujú funkcie, ako napríklad is.na(), ktoré vám pomôžu tieto chýbajúce hodnoty nájsť a pracovať s nimi.

Sara: Dobre, mám svoje dáta v R. Čo teraz? Môžem hneď spustiť nejaký zložitý štatistický model?

Sam: Mohli by ste, ale to by bola chyba. Prvým krokom je vždy prieskumná analýza dát, alebo EDA (Exploratory Data Analysis). Musíte sa so svojimi dátami najprv zoznámiť.

Sara: Ako rande s mojimi dátami?

Sam: Presne tak! Chceš zistiť, či v nich nie sú chyby, aké majú rozdelenie, či tam nie sú nejaké nečakané trendy. R má na to skvelé nástroje. Funkcia summary() vám poskytne základný prehľad o všetkých premenných naraz: minimum, maximum, medián, priemer...

Sara: To je užitočné. Takže namiesto toho, aby som sa prehrabávala v tabuľkách, môžem jedným príkazom získať rýchly prehľad. To znie ako začiatok nášho ďalšieho rozhovoru.

Sara: And that ability to spot patterns is exactly what we're diving into now with statistical modeling. It sounds intimidating, Sam, I have to admit.

Sam: It really does, but it's one of the most powerful tools you can have. Think of it this way—a statistical model is like a recipe for reality.

Sara: A recipe? Okay, I'm intrigued. What are we baking?

Sam: We're baking predictions! Every good recipe has two parts: the ingredients you control, and the little bit of randomness... you know, maybe the oven runs a bit hot today.

Sara: Okay, so ingredients and randomness. How does that translate to a model?

Sam: Great question. The ingredients part is what we call the 'systematic component'. It's the predictable relationship we're studying. Like, for every extra hour you study, your score goes up by a certain amount.

Sara: That's the part we can write down as an equation, right? Like y equals mx plus b from algebra class?

Sam: Exactly! It's that classic regression model. We have our outcome, y, which equals some baseline value, plus the effect of our variable, x. That's the deterministic, predictable part.

Sara: But life isn't perfectly predictable. My score won't go up by the exact same amount every single time. That’s where the randomness comes in?

Sam: You got it. That's the 'stochastic component', or what scientists call 'error'. It's not a mistake! It’s just all the other random stuff we can't measure that influences the outcome. It's the oven running a little hot.

Sara: So the model is part predictable pattern, part random noise. That makes a lot more sense.

Sam: And the simplest, most common type of model is the Linear Model, or LM. This is your workhorse for so many analyses.

Sara: Is this just for straight-line relationships?

Sam: Not at all, and here's the surprising part. A model is 'linear' if it's linear in its *parameters*—the numbers we're estimating. The variable itself can be squared, or a log, or a sine wave!

Sara: Wait, so you can model a curve with a 'linear' model? How does that work?

Sam: Yep! As long as you're just adding the terms together, it still counts. So you can test if a relationship is actually a curve by adding a squared term. For instance, maybe studying helps up to a point, but then you get diminishing returns. A quadratic term would capture that curve.

Sara: That’s a game-changer. So you’re not stuck with just lines. What if the *randomness* part doesn't behave nicely?

Sam: Ah, now you're thinking like a true statistician! That’s when we move on to the big sibling: the Generalized Linear Model, or GLM.

Sara: Generalized... so it handles more situations?

Sam: Precisely. A standard linear model assumes the random noise follows that perfect, bell-shaped normal distribution. But what if your data doesn't?

Sara: Like what? Give me an example.

Sam: What if you're counting something? Like the number of birds in a park. You can't have half a bird, and you can't have negative birds. The data is all whole numbers, often clustered near zero.

Sara: Right, that doesn't sound like a bell curve at all.

Sam: It's not! For count data, we might use a Poisson distribution. If you're looking at proportions, like the percentage of students who passed a test, you might use a Binomial distribution. GLM lets you choose the right distribution for your data.

Sara: So it has three parts: the variables you're testing, the distribution you choose for the randomness, and something called a 'link function'? What's that?

Sam: The link function is just a clever bit of math that connects your variables to the mean of your chosen distribution. It's the secret sauce that makes it all work. It transforms your data so the linear model part still applies.

Sara: So to recap: Linear Models are for data with bell-curve-shaped randomness, and Generalized Linear Models are for everything else—counts, proportions, you name it.

Sam: You've nailed it. It’s about picking the right tool for the job.

Sara: Okay, so we have all these potential variables we could include. How do we decide what makes it into the final model? Do we just throw everything in?

Sam: You could, but that’s a messy approach! It’s called a maximal model. A better way is to be strategic. There are two main philosophies.

Sara: A modeling philosophy? Sounds profound.

Sam: It is! First is 'forward selection'. You start with nothing, and you add variables one by one, keeping them only if they significantly improve the model.

Sara: Like building a house brick by brick.

Sam: Perfect analogy. The other is 'backward selection'. You start with everything—the maximal model—and you chip away, removing the least important variables one at a time.

Sara: Like a sculptor carving away marble to find the statue inside. I like that one.

Sam: Me too. You keep simplifying until every single thing left in your model is statistically significant. This is called the 'minimal adequate model'. It follows the principle of parsimony.

Sara: Parsimony... meaning the simplest explanation is usually the best?

Sam: Exactly. A good model is a useful simplification of reality. We want it to be as simple as possible, but no simpler. The goal isn't to be perfect; it's to be useful.

Sara: And once we have that model, we can't just trust it blindly, right? We need to check its work.

Sam: Absolutely. And that process of model criticism, of kicking the tires on your model to check its assumptions, is our next critical step.

Sara: So that makes sense for comparing two different groups, like we discussed. But what happens when your experiment is more complex? What if you have three groups… or five?

Sam: That's the perfect question to set us up for our next topic, Sara. You're teeing up the powerhouse of statistical tests... ANOVA.

Sara: ANOVA. I've definitely heard that term. It sounds a little intimidating.

Sam: It sounds it, but it's not! It stands for Analysis of Variance. And it’s your best friend when you need to compare the means of three or more groups at the same time.

Sara: Okay, but hang on. If a t-test compares two groups, why can't I just run a bunch of t-tests between all my different pairs of groups?

Sam: A very logical question, and one every student asks! The problem is something called error inflation. Every time you run a test, there's a small chance you'll find a significant result just by random luck... a false positive.

Sara: Right, that's the p-value, like that 5% chance.

Sam: Exactly. Now, if you run one test, it's a 5% chance. But if you have five groups, you'd have to run ten different t-tests to compare every pair. Those small chances of error add up really, really fast.

Sara: Oh, I see. So your odds of getting a bogus result get way too high. It's like buying more lottery tickets... eventually one might hit, but it doesn't mean you have a winning system.

Sam: That's a great analogy! ANOVA is the tool that lets us test all the groups at once with a single, controlled error rate. It protects us from chasing ghosts in our data.

Sara: Okay, I'm sold. Protect me from the ghosts. How does it actually work? Can you give me an example?

Sam: Of course. Let's talk about an experiment run by a researcher named Stano Pekár. He wanted to find the best diet for growing cockroaches.

Sara: Cockroaches? Alright, I'm listening... I guess they need to eat too.

Sam: They do! So, he had five different diet types. There was a control group, two diets enriched with lipids—or fats—and two diets enriched with protein.

Sara: So five groups in total. A perfect job for ANOVA.

Sam: Precisely. The biological question was simple: Does the nutritional quality of a diet affect the size of an organism? In this case, the body weight of cockroaches.

Sara: So what were the statistical hypotheses? Is it similar to a t-test?

Sam: It is. The null hypothesis, or H0, was that the average weight would be the same across all five diet groups. There’s no difference.

Sara: And the alternative hypothesis?

Sam: The alternative, HA, was that the weight is significantly different in *at least one* of the groups. Notice it doesn't say *all* groups are different, just that they aren't all the same.

Sara: That’s a key distinction. So ANOVA gives you a kind of 'yes' or 'no' answer to the big question: Is there *any* difference here at all?

Sam: Exactly. The main variable we're looking at is 'DIET', which is our categorical factor with five levels. And the outcome we're measuring is 'weight'. That's a classic one-way ANOVA setup.

Sara: So you run the analysis. What does the output look like? What tells you if you've found something interesting?

Sam: You get what's called an ANOVA table. It gives you a few key numbers, but the one you really care about is the F-value and its corresponding p-value.

Sara: And a super small p-value is good, right? It means we can reject the null hypothesis.

Sam: You got it. In the cockroach study, the p-value was tiny... way less than 0.001. That's our green light. It tells us that diet has a highly significant effect on cockroach weight. The groups are not all the same.

Sara: Okay, great! So... protein makes them bigger? Or lipids?

Sam: Ah, and that’s the catch. ANOVA tells us *that* there is a difference, but it doesn't tell us *where* that difference is. We only know the five groups aren't identical.

Sara: So what's the next step? How do you figure out which diets are the winners... or losers?

Sam: For that, we use what are called post-hoc tests, which literally means 'after this'. These tests are designed to compare all the individual pairs of groups, but they do it in a smart way that controls for that error inflation we talked about.

Sara: Okay, so it's like a smarter, safer version of running all those t-tests.

Sam: Exactly. A very common one is the Tukey HSD test. It goes through and compares the control diet to the lipid diet, the control to the protein, the lipid to the protein... every single pair.

Sara: And what did it find for our cockroach friends?

Sam: It showed that both protein diets led to significantly heavier cockroaches than the control and lipid diets. It also showed that the two protein diets weren't significantly different from each other, and neither were the two lipid diets.

Sara: So you can actually group things together then? If the two protein diets had the same effect, you can just call them 'protein' from then on?

Sam: Yes! That's a fantastic practice called model simplification. We found that protein1 and protein2 were similar, and lipid1 and lipid2 were similar. So we can group them into just three categories: Control, Lipid, and Protein.

Sara: That makes the story so much clearer. Instead of five things, you can just say that protein diets lead to the biggest cockroaches, followed by lipid diets, and then the control diet.

Sam: That's the goal. You start with the full model, then you use the data to simplify it into the clearest, most accurate story possible. You test this simplification to make sure you aren't losing important information, and in this case, we weren't.

Sara: I love that. It feels like you're refining your answer, getting right to the core of what's happening. It’s not just about running a test, but about telling a clear story with the results.

Sam: That's what it's all about. And this whole process, from the overall ANOVA test to the post-hoc comparisons and simplification, gives you a really powerful and responsible way to analyze more complex experiments.

Sara: This is a total game-changer for designing projects. But what happens if you have a variable that isn't a neat category, like diet? What if you also measured something continuous, like the temperature of the lab?

Sam: Now you're thinking like a pro statistician. When you want to mix categorical and continuous variables, you're moving into the world of ANCOVA, and that's exactly what we'll tackle next.

Sara: So, those initial graphs showed us that a simple straight line isn't going to cut it for predicting crop yield. The relationships are more... bendy.

Sam: Exactly. And we've also got another classic problem to solve: collinearity. Look at the pH and Phosphorus variables. They're correlated. They're basically holding hands and telling us the same story.

Sara: Two variables that are a little too close for comfort. Why is that a problem for our model?

Sam: Well, it confuses the model about which variable is actually responsible for the effect. It can't tell them apart. So, we need to deal with that before we go any further.

Sara: Okay, so how do we get these variables to stop copying each other? Do we just kick one of them out?

Sam: We could, but that's like throwing away data. A smarter way is to combine their effects into one new, powerful variable. We use a technique called Principal Component Analysis, or PCA for short.

Sara: A new super-variable? I like the sound of that.

Sam: It is pretty super. We use the princomp function in R on our pH and Phosphorus variables. And the summary it gives us is fantastic. Check this out: the first component, Comp.1, explains over 85% of the combined variance of those two variables.

Sara: Wow, so that one component captures almost everything from both of them?

Sam: Precisely. So we just create a new variable, we can call it PpH, that holds the scores of that first principal component. And just like that, our collinearity problem between pH and P is solved.

Sara: Alright, our variables are no longer codependent. What's the next step? Building the actual model?

Sam: You got it. And because we saw those curves in the data, we can't just use a simple linear model. We need a quadratic model. We have to give it the ability to bend.

Sara: So what does that involve? Adding more terms?

Sam: A lot more terms. For every variable, like the number of overwintering plants, we include the variable itself *and* the variable squared. Then, to be extra thorough, we also include every possible two-way interaction between all our variables.

Sara: That sounds… huge. Like you're throwing the entire kitchen sink at it.

Sam: That's the perfect analogy. And when we look at the analysis of variance table for this first massive model, m1, you can see the result. Most of the p-values, especially for the interactions, are really high. They're not statistically significant.

Sara: So our giant 'kitchen sink' model is full of junk. How do we clean it up?

Sam: This is where we get strategic. Before we simplify, we need to address another issue. Our variables are all on different scales. pH is around 5, while Potassium is over 100. It's like comparing apples and watermelons.

Sara: That doesn't seem fair. How does that affect the model?

Sam: It makes it impossible to compare the coefficients. A small change in a large-scale variable might look less important than a big change in a small-scale one, even if it's not. The solution is called centering and scaling.

Sara: Let me guess: you subtract the mean and divide by the standard deviation?

Sam: You're on fire today, Sara! That's exactly it. We use the scale() function on all our predictor variables. Now, every variable has a mean of zero and a standard deviation of one. They're all speaking the same language.

Sara: Okay, all our variables are scaled and ready. Now we can build the model again and get rid of the junk?

Sam: Yep. We start with the big model again, but this time using our new scaled variables. We'll call it m3. Then, we begin a process of backward elimination. We remove the least significant term—the one with the highest p-value—and refit the model. Over and over again.

Sara: That sounds like it would take forever.

Sam: It would if you did it by hand! We just use a function to do it for us. After 20-something steps, we arrive at our final, lean model, m26. The ANOVA table shows only the heavy hitters are left: w1, pH1, K1, and pH1 squared.

Sara: And now for the big reveal. Let's look at the summary of that final model.

Sam: Here's the magic. Because we scaled everything, we can now directly compare the estimated coefficients. We can see the number of wintering plants (w1) has a coefficient of 0.28, which is the largest positive effect.

Sara: And the quadratic term for pH (I(pH1^2)) has a strong negative effect. That shows us the curve we saw in the plots!

Sam: Exactly. The model fits the data. Our adjusted R-squared is about 0.40, meaning this simple model explains 40% of the variation in yield. That's a solid win for predicting crop outcomes.

Sara: That's amazing. By cleaning, scaling, and simplifying, we've built a model that really tells a story. This is the kind of edge that makes a difference. And it all starts with looking closely at your data.

Sara: Alright, that brings us to our final topic. And it's a big one... Biostatistics. I think this word alone makes students nervous.

Sam: It definitely can! But trust me, it’s not as scary as it sounds. The key is just breaking down the design of the study, piece by piece.

Sara: Okay, so let's try that with this example. We're looking at a study connecting bacterial toxins to cancer.

Sam: Exactly. The core idea is simple: researchers think a specific toxin might be a signpost for cancer. So, they designed an experiment to test it.

Sara: How did they set it up?

Sam: They had patients with four different diseases. Two were cancerous, and two were not. And they had 20 patients for each disease.

Sara: So that's eighty patients in total. And they were measuring toxins from four different types of bacteria?

Sam: You got it. Now here’s the important part. In each group of 20 patients, they tested the toxin from each bacteria species on five people. So, 5 patients for bacteria A, 5 for bacteria B, and so on.

Sara: Ah, so that’s what '5 replications' means! It’s just 5 different patients for each test. That makes it so much clearer.

Sam: See? Once you decode the language, the logic is right there. That's the edge you need—don't get intimidated, just break it down.

Sara: That’s a perfect summary for our entire discussion today. Break the problem down into manageable parts. Sam, thank you for clarifying everything for us.

Sam: My pleasure, Sara. Keep up the great work, everyone!

Sara: And that’s a wrap on the Studyfi Podcast. Thanks for listening, and happy studying!