 Okay, so at the end of the last video, we had a data frame called mod specs, which looked like this as it's printed to the console. It has 96 rows and five columns, and each row gives us the information that we need to specify a model that we want to run. Now we have this specification grid. The next step is to take one of these rows and use it to create a regression model and then extract the results that we need. We're only really interested in the coefficient for the association between cognitive ability and BMI. So we don't need to return the full regression model object, as this would include things like the data that was used to run the model. We're gonna write the code out for one model first to be as general as possible, so that it would work regardless of which row of the specification grid we fed into it. Once we've written the code once, we're gonna turn it into a function, and that function will take a single input spec ID so that it can extract the appropriate row from the specification grid and then run the appropriate model and get the appropriate results. Again, it's easier to use spec ID than to pass all the information in separate arguments to the function as this can get cumbersome, particularly where the number of columns in the specification grid is large. It is also more adaptable if we want to add further columns to the specification grid down the line. Let's start by setting spec ID to one so that we have a particular row of data to work from. There we go. Next, we want to use spec ID to obtain the relevant row from the specification grid. Here, I'm just going to use the filter function to return rows where a particular condition is true. So we're gonna take mod specs, and then we're gonna filter to where the column spec ID is equal to spec ID. Now, this bit of code that I've just run isn't going to work. And the reason for this is that there's an external object called spec ID, which contains the value one, but then there's also a column within mod specs which has the numbers, sorry, there's a column within mod specs with the column called spec ID, which goes from one to 96. By default, the functions within the tidyverse are going to use the column within the data frame rather than an external object. So this statement just says, give me back those rows where the column spec ID is equal to the column spec ID. If we want to refer to an external object, we can do so using the bang bang operator, which is this exclamation exclamation mark. This explicitly tells us to look for the object in the external environment. So now when we run this, we just get one row back where spec ID is equal to one. Let's save this object as spec, the specification that we want to use so that we can refer to it easily later. Okay, this is what spec looks like. We can extract columns from the spec using the dollar sign operator. So to get the foop, the age of follow-up, we can just do spec dollar foop. To get the cognitive ability variable, we can do spec dollar sign cog var. Now we're going to take the information contained in this single row of data to run a regression model. There's a few things we need to do. First, we need to subset the DF data frame to get back just those observations we need to run the model. That is the data from a particular cohort and follow-up page. Second, we need to specify the model formula. That's the names of the outcome and the exposure variables and the sets of the covariates control variables that we want to add. Third, we want to run the model and extract the results that we need. In this case, the coefficient for the cognitive ability variable. Let's first subset the DF data frame. From the spec, the observations that we need are from the age 15 follow-up for the baby boomer cohort. We can do this subsetting using the filter function. So we'll take DF and we'll pipe it into filter. We want to keep where cohort is equal to spec cohort. This is given us back too much information. It's given us 30, 30,000 rows, but it's given us observations at age 15, 25, 35, and so on. We can add multiple filter statements within one function call just by separating them with columns. So with commas, these are combined with and. So we're going to keep the rows that have cohort equal to baby boomer and fob equal to 15. Great, so now we've got back the 5,000 rows that we need. They're just from this age 15 follow-up. Let's save this data frame as DF mod so that we can refer to it later. Okay, so the next thing we need to do is specify the formula for the model that we want to run. I'm going to use the glue package to do this because it's got a very straightforward syntax for constructing strings. Glue is part of the tidyverse, but it's not a core tidyverse package. So it needs loading separately. So we can just use library and then glue to load it. The main function within glue is called itself called glue. I'm going to create an object called x, which is going to be a vector one to seven, yeah, containing the integers one to seven. I'm going to quickly use this just to demonstrate how glue works. So that's x saved. And then we've got one, two, three, four, five, six, seven. To create a string using glue, we just write out some text and then within curly brackets, we provide the name of an object or we write a function. Glue will run R in the background and place the results that are within this curly brackets directly into the string. So for instance, if we use glue to create a string containing the mean of one to seven, we can do that using curly brackets mean x, and we can add some explanatory text to that. So the mean of one, two, seven is four, because within curly brackets, it's mean x. The mean of the object x is the mean of the vector one to seven, which is four. We can use this glue function to create model formulas. So we want to regress BMI on a measure of cognitive ability plus some control variable. So let's just have a quick look at spec again. So the first thing we can do is add in the cog var. So let's just do BMI curly brackets spec cog var. Let's just run that. Now you can see we've got BMI tilde cog non-verbal. So we've got the right variable specified there. We also want to add some control variables and we can specify another set of curly brackets, do mod covars, double square brackets, and then spec covars. So this will replace spec covars with basic. Mod covars basic will just be the set of control variables for the basic model. So now when we run this, we've got BMI cog non-verbal plus age plus female plus social class. And this gives us the correct formula string. We need to do one more thing though, which is to change this string from a string to a formula type object. And we can do this using the as formula function. Regression functions like LM, which you use for linear regression, require a formula object to specify the model. So let's just quickly pipe this into the as formula function. You can see it has an environment now, which shows you that it's a formula type object. Let's just quickly save that as mod formula. Okay, so we now have the data in DF mod and a model formula in mod formula, which is all we need to run the regression model. Let's do that and save the result as an object called mod. So we have mod, LM, mod formula, and data equals DF mod. Let's quickly just call summary mod to see the results. So we've got an intercept, we're coefficient for cognitive verbal and coefficients for the other control variables. Now this mod object is quite complicated. It's got the data that we use to run the regression model and lots of other extraneous information. Really, as I said before, we just need the coefficient for cognitive verbal and some of the related estimates like the standard error for that coefficient, the p-value and the confidence intervals. To extract just this information, we can use the broom package. Broom is another tidyverse package, but it's not a core one, so it needs loading explicitly, so library broom. Broom contains a set of functions that extracts model information and put the results into data frames, which makes it particularly useful to work with. So we're going to use the tidy function from broom, which takes a model object and returns a data frame with the coefficients and related estimates. So that's tidy mod. We're also just going to specify the confidence argument to return the confidence intervals. So when we run that back, you'll see it returns a table with a row for each of the different coefficients of the model. So we've got one for the intercept, CognomVerbal and the three different control variables. This is still a bit more information than we need. So let's first use the filter function to get just the row for the cog file that we're interested in. So we can pipe this into filter and we can do term equals spec cog var. There we go. It's also got a bit too much information in terms of the number of columns. So let's just use the select function to return just those columns that we need. So that's the estimate, the standard error, the p-value, the confidence intervals, comp flow and comp high. Great, that's everything we want. So in a few lines of code, we've been able to take a spec ID, use it to prepare data and run a regression model and then extract to the appropriate results. Let's turn the code above into a function now so that it can be reused. Let's call this function get a lamb and it will take a single input spec ID. So we do get a lamb function and then we'll just name the particular argument that we want, single argument called spec ID. And I'm just going to copy and paste this stuff from above and put it straight into the get a lamb function. I'm going to tidy this up a slight bit. So I'm gonna rename the result of the tidy function as ret. I'll put a return statement in as well. I'll remove summary mod because we no longer need it. I don't need this bit about loading library glue and using the X's, so I can get rid of that. And also the stuff where I've just repeatedly called on spec. Okay, so let's just save that as get a lamb and then we can have a quick look at whether this works. So let's just run get a lamb one and we've got the same result as we had before. So an estimate of 0.1969. We can also just change the input to 15 to get the result of the 15th row. So now we've got 0.48. Great, and there you have it. That's the results for the first specification of the 15th. In the next video, we'll see how we can precisely run this code for all the specifications in our specification grid. Thank you.