Download presentation
Presentation is loading. Please wait.
1
R fitting to functions other than lines
2
Power-Law from log-log linear model
3
Recall the data used in an earlier lab that varied the length of a pendulum and measured its period (the time to swing back and forth) Length (cm) Period (s) 108 2.080 89 1.888 58 1.534 44 1.3352 25 1.0199
4
The Excel fit to a power-law yielded
5
Entering the same data into R looks like
# copy and paste length = c(108, 89,58,44,25) period = c(2.080, 1.888, 1.534, , )
6
Plotting the data in R looks like
7
Log-log linear model That number looks familiar from the Excel Power-law fit. It was the power.
8
Can’t take log of 0 or negative numbers
One cannot take the log of zero or a negative number Recall on the test we were plotting the kutosis of dice simulations versus how many dice were rolled. But the kurtosis was negative. Thus we had to take the absolute value and plot and fit that. Behind the scenes in Excel there are log’s being taken, and it cannot handle the negative numbers
9
Isolating the power
10
Breaking it down pend_fit is the name we gave to the model
The model has coefficients (what we usually call slope and intercept) Our x’s are log(pendulum$length) so we are getting the coefficient associated with the x – what we would normally call the slope Originally it was the power
11
Breaking it down y = A x^b log(y) = log(A x^b)
log(y) = log(A) +log(x^b) log(y) = log(A) + b log(x) We see that the slope in log(y) versus log(x) is b – what was originally the power in the power law By log() in R we mean the natural log ln() intercept = log(A) exp(intercept) = A The exponential function and natural log functions are inverses
12
Obtaining the Coefficient
13
Displaying fit equation
14
Displaying the fit curve
15
Exponential Fit from log linear model
16
Data from on how air pressure depends on altitude
Altitude (miles) Pressure (psi) 14.696 11.022 7.348 1.4696
17
Plot with Exponential fit from Excel
18
Enter the data into R #copy and paste
altitude = c(0, , , , , , , , ) pressure = c(14.696, , 7.348, , , , , , )
19
Plotting the data in R
20
This time take log(y) but not log(x) and obtain a linear model
Once again the “slope” number looks familiar when compared to what we got from Excel
21
Cannot take log(0) When we used this data on the first test, some students got their x and y axes confused. In this data x=0 is perfectly fine, but y=0 is not. That is why for those with the wrong axes, the exponential fit was unavailable in Excel
22
Isolating the factor
23
Determining the coefficient
24
Displaying the equation
25
Displaying the curve
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.