R fitting to functions other than lines
Power-Law from log-log linear model
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
The Excel fit to a power-law yielded
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, 1.3352, 1.0199)
Plotting the data in R looks like
Log-log linear model That 0.4873 number looks familiar from the Excel Power-law fit. It was the power.
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
Isolating the power
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
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
Obtaining the Coefficient
Displaying fit equation
Displaying the fit curve
Exponential Fit from log linear model
Data from on how air pressure depends on altitude Altitude (miles) Pressure (psi) 14.696 1.708712 11.022 3.409091 7.348 5.204545 4.898177 10.02386 1.4696 19.20095 0.14696 30.1161 0.014696 43.16269 0.00147 53.61288 0.000147
Plot with Exponential fit from Excel
Enter the data into R #copy and paste altitude = c(0, 1.7087, 3.4091, 5.2045, 10.0239, 19.2010, 30.1161, 43.1627, 53.6129) pressure = c(14.696, 11.022, 7.348, 4.8982, 1.4696, 0.14696, 0.014696, 0.00147, 0.000147)
Plotting the data in R
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
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
Isolating the factor
Determining the coefficient
Displaying the equation
Displaying the curve