- t phải mất ba đối số: data, x, và y. Hàm sẽ tạo một biểu đồ phân tán
với một đường hồi quy và in tóm tắt của mô hình hồi quy tuyến tính. x và
y trong trường hợp này là glucose tại thời điểm 0 giờ và sau 2 giờ
t <- function(data, x, y) {
library(ggplot2)
model <- lm(formula = paste(y, "~", x), data = data)
print(summary(model))
scatter <- ggplot(data, aes(x = .data[[x]], y = .data[[y]])) +
geom_point() +
geom_smooth(formula = y ~ x, method = "lm") +
labs(x = x, y = y)
return(scatter)
}
d <- data.frame(
y = c(5.9, 5.3, 4.6, 6.2, 6.0, 6.4, 7.6, 5.9, 7.5, 6.2, 6.9, 5.6, 5.1, 5.7, 5.0, 5.2, 7.7, 8.0, 7.7),
x = c(3.9, 4.7, 3.7, 4.6, 5.4, 4.7, 4.1, 3.1, 6.1, 5.3, 5.6, 4.7, 3.9, 4.7, 4.0, 4.2, 6.2, 5.8, 5.0)
)
# Call the function with the data frame 'd'
plot_scatter <- t(data = d, x = "x", y = "y")
##
## Call:
## lm(formula = paste(y, "~", x), data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.91970 -0.61615 -0.05247 0.29533 1.86881
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.3930 1.0674 2.242 0.03860 *
## x 0.8142 0.2227 3.656 0.00195 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.8063 on 17 degrees of freedom
## Multiple R-squared: 0.4402, Adjusted R-squared: 0.4073
## F-statistic: 13.37 on 1 and 17 DF, p-value: 0.001955
print(plot_scatter)
