Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am stuck for time for this lab involving R Studio, help would be greatly appreciated --- title: Fitting Models to Data in R author:

I am stuck for time for this lab involving R Studio, help would be greatly appreciated

--- title: "Fitting Models to Data in R" author: output: html_document ---

```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) library("mosaic") ```

One of the most powerful capabilities of R is model fitting, that is, figuring out the correct parameters that make a chosen family of models fit a paricular data set the "best" (if you take a stats course, you will learn what "best" means and lots of measurements of "best", but that is not the focus for this class).

To do model fitting, you really need to:

1. Get data 2. Guess a model to fit the data (maybe based on a graph you have made, or based on some other information or suspicion) 3. Estimate the parameters in your model from the data, and 4. Compare the model to the data.

Let's do this through some examples.

## Water Data

Recall in the Data Activity, you loaded the stan-data.csv data, which records the time and temperature of a cooling cup of coffee. Let's remind ourselves about a plot of these data and remember that you were supposed to fit the exponential model y(x)=27+Pe^(kx) to the data. If you notice at time=0, temp=98.2, which can help you find P=71.2. If you plug in another point, you can find the decay parameter k (using the point time=1, temp=94.4, the corresponding decay parameter is -0.0548). Plotting this function on the data points, we see it is not a very good fit (make sure you understand each line of code below).

```{r} WaterData = fetch::fetchData("stan-data.csv") plotPoints(temp~time,data=WaterData) OldModel=makeFun(27+71.2*exp(-0.0548*t)~t) plotFun(OldModel(t)~t,add=TRUE, col="red") ```

Our goal today is to come up with better models to fit the data using R.

Even though these data do not look linear, let's try to fit a linear function F(x)=mx+b to the data. We will do so with the `fitModel` command in R by using the WaterData to find the best linear function.

```{r} F = fitModel(temp ~ m*time+b, data = WaterData) ```

There are a few things to notice here. First, the tilde notation is similar to what we are used to: dependent variable to the left, and a dependency on the right. Here, the dependency is the formula we are guessing, in terms of the independent variable. Second, we are inputting the actual data into the fitModel command to find the best function for this data. Third, the names of the independent and dependent variables must be exactly correct. If you type `Time` instead of `time` or `Temp` instead of `temp`, your command will fail. Also, remember that we specify `data=WaterData` so that R knows what data variable to look in. Finally, note that we capture the output of `fitModel` into something. I have called it `F`, though you can call it anything you want. `F` will be a function, just like a function you'd create with `makeFun`. You can do to it anything else you'd do to a function. For instance, you can plug in to it, `plotFun` it, and so forth.

**Type coef(F) into a code block below. Interpret the output.**

**Execute F(7.9) to see that it returns an output. (Hence, the function F is already created, you do not need to use makeFun with the coefficients to define a new function.)**

**Plot the WaterData with the function F added to the plot in red. Is this a good fit?**

Now, let's go back to our exponential model, G(x)=A+Pe^(kx), where we now will let R try to get the best values for all of the parameters (including A).

**Run a fitModel command for G.**

```{r error=TRUE} G = fitModel(temp ~ A+P*exp(k*time), data = WaterData) ``` You should get a confusing error message (something about a singular gradient), even if you type everything correctly. In general, if your fitModel command is correct but fails to produce an output (by giving you this error), you can assume that R needs more information. The specific information that is needed is that R wants some guesses for some of the parameters. Luckily, we already have some guesses for parameter values from the previous Data Activity. We can put these parameters as an input to the fitModel command in a start list: start=list(A=27,P=71.2,k=-0.0548).

**Rerun the fitModel command for G with the start list included.**

Yay! This should now work.

**Run coef(G) and compare these values to your start list.**

**Execute G(7.9). Look at the temperature values when time=7 and time=8. Do you think F(7.9) above or G(7.9) is a better fit to the data?**

**Plot the data, the OldModel (from the data activity), and G.**

Where is OldModel a better fit than G?

When does G overestimate the data? Underestimate?

##LA Sunrise

In Moodle, there is a file called LASunrise.csv which gives the number of minutes after 4 a.m. until sunrise in Los Angeles, California, from January 2010 through December 2011. Recall the directions for uploading a .csv file from the Data Activity:

1) The first thing we need to do is upload the data set to our files. In the lower right panel, click on the files tab and then the upload button. You will be prompted to pick a file. 2) Once you have uploaded the file, it should appear in your files list. Click on its name and chose import dataset from the small pop up menu. 3) A window will appear with the data set. If you'd like to change what the data set is called, you can do so in the lower right portion of the window. 4) Hit the import button! You should see the data set in your environment tab.

**Load the LASunrise.csv data and plot the Time depending on Month.**

Notice, that it looks like a periodic function.

**Fit the periodic function H(x)=Acos(w(x+h))+C to these data. You may need a start list. Use your knowledge from Unit 1 to estimate starting parameters. Plot this function in purple on the LASunrise data.**

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions