1170:Lab10 November 6th, 2012 Another look at the lung model. In this lab we will take our first look at fitting a model to data with R. We’ll look at a data set and try to fit it with three different functions. Then we’ll take two of those models and extrapolate to predict lung function. The key new functions that we’ll learn about will be lm and abline, which create linear models and plot best fit lines. 1 The lung model In lab4, we used R to simulate a human lung. In essence, the model is a discrete time dynamical system for c, the concentration of oxygen in the lung. ct+1 = ct (1 − q)(1 − a) + γq (1) In the equation above, q represents the proportion of air that is exchanged with every breath and r represents the proportion of oxygen that is aborbed. Therefore they must both be less than one. We’re going to assume that both q and a are both functions of a third parameter T , the length of each breath. Letting q(T ) = rT and leaving A(T ) unspecified, we have: ct+1 = ct (1 − rT )(1 − A(T )) + γrT (2) This means that once the lung has equilibriated, the amount of absorption per breath will be c∗ A(T ) = γrT A(T ) 1 − (1 − rT )(1 − A(T )) (3) and the rate of oxygen absorption is γrA(T ) c∗ A(T ) = T T (1 − (1 − rT )(1 − A(T ))) We’ll plot these functions later, so we’ll take the time right now to write the code to do that. ... 2 Finding A(T) The book mention 3 possible forms for A(T ). 1. A(T ) = α T: Oxygen absorbed at a constant rate 2. A(T ) = α(1 − e−kt ): Oxygen saturates at particular level 3. A(T ) = αT 2 /(k + T 2 ): Absorption starts slowly, speeds up, then levels off. 1 (4) Suppose we measure the absorbtion rates of a human test subject and make the appropriate normalizaT A(T) .0623 .0127 .4985 .1058 .9957 .1612 .1351 .0280 tions. We get the following data. .8165 .1700 .5856 .1322 .3301 .0711 .2294 .0529 .2568 .0477 .0687 .0148 Store this data in two vectors >T<-c(.0623,.4985,... >A<-c(.0127,.1058,... Then plot them >plot(T,A) The data look roughly linear. We can find and plot the slope and intercept of the best fit line like this >lm(A~T-1) >abline(lm(A~T-1)) Save this plot and record the slope that lm gives you. The function ’lm’ stands for linear model and is a very powerful function in R. The usage >lm(A~T-1) means that we are looking at A as a function of T and we want to ignore the intercept. If we wanted it to find the intercept also we would omit the ’-1’. Next, we can fit an exponential curve. Although R does have non-linear curve fitting, we won’t bother with them for now and we’ll make this linear too. We suppose that A(T ) = α(1 − e−kt ) and that k for this model about .3. Then we’ll define >ET<-exp(-.3*T) Then we’ll find a linear model. Note that the plot looks linear but on the x-axis is e−.3∗t instead of .3. >plot(ET,A) >lm(A~ET) >abline(lm(A~ET)) The value you find for the intercept is actually the α for the exponential model. Note, that this is different from the first value of α, that’s alright, they have different meanings. Save this second plot and also save your new value for α 3 Tasks for this week Include the two graphs from above. Both plots should show data points with a best fit line through them. Plot oxygen per breath and rate of oxygen uptake. The two different curve fits to our data give us two different functions of A(T ). Plug those into the formulas from the first section. Plot both quantities 2 for T = 0..1 with γ = 5 and r = .5. Make sure everything is labeled appropriately. You should have 4 graphs for this part. Slow start for absorption Suppose A(T ) = αT 2 /(k + T 2 ).Use lm to make a linear model of 1/A as a function 1/T 2. You’ll have to define a new variable >newT<-1/T^2 Save the resulting graph. T The slope and intercept from lm give you 1/α and k/α respectively. Do you think this model fits your data? 3