Data Analaysis and Graphs practice

Author

Matthew Miklaucic

Graphing Practice

Importing data:

url = "https://raw.githubusercontent.com/thomasgredig/QuartoBasics-545/main/data/BDBL-20160303-NMR-FID1Data.CSV"
d = read.csv(url, header=FALSE)
head(d)
               V1                 V2 V3        V4    V5 V6
1   Record Length       2.500000e+03 NA -0.002480 0.064 NA
2 Sample Interval       2.000000e-06 NA -0.002478 0.072 NA
3   Trigger Point 1.240000000000e+03 NA -0.002476 0.080 NA
4                                    NA -0.002474 0.064 NA
5                                    NA -0.002472 0.072 NA
6                                    NA -0.002470 0.072 NA

Graphing data:

library(ggplot2)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
d %>% 
  mutate(t.ms = V4*1000) %>%
  mutate(V.V = V5) -> d1
d1 %>%
  ggplot(aes(t.ms, V.V)) + 
  geom_point(color='red') + 
  xlab("t (ms)") + ylab("V (V)") +
  theme_bw()

Fitting data:

Module:

\[ V(t)=V_0e^{-t/T} \]

# select data after time = 0
d2 = d1 %>% filter(t.ms>0.2)
# fit the data with some starting values
fit = nls(data = d2,
          V.V ~ A+ V0*exp(-t.ms/T),
          start = list(A=0.15, V0 = 1, T = 0.8))
summary(fit)$coef     # output the fitting parameters
    Estimate  Std. Error    t value     Pr(>|t|)
A  0.0225856 0.002846452   7.934652 4.956704e-15
V0 1.1406403 0.004981881 228.957733 0.000000e+00
T  0.7530520 0.008116360  92.781978 0.000000e+00
d.fit = data.frame(
  t.ms = c(0, d2$t.ms),
  V.V = predict(fit, list(t.ms = c(0,d2$t.ms)))
)

d1 %>%
  ggplot(aes(t.ms, V.V)) + 
  geom_point(color='red') + 
  geom_line(data=d.fit, color='blue', linewidth=1) + 
  xlab("t (ms)") + ylab("V (V)") +
  theme_bw()