Wednesday, October 25, 2023

Module #10 Assignment

 

Module 18 Assignment


9.1 Analysis of Variance (ANOVA) and Regression Coefficients:


To conduct an ANOVA and interpret the coefficients for the model using the "cystfiber" data, use:



if (!exists("cystfiber")) {

  data("cystfibr")

}


model <- lm(formula = spemax ~ age + weight + bmp + fev1, data = cystfiber)


anova_result <- anova(model)


print(anova_result)


coefficients(model)


If the "cystfibr" data hasn't previously been loaded, this code loads it first. We then used the above formula to fit a linear regression model. To perform an ANOVA and present the findings, we utilize the anova function. Lastly, we use the coefficients function to print the regression coefficients.



9.2 Analysis of Log-Transformed Data and Regression Coefficients:


To analyze log-transformed data ad regression coefficients, you can log-transform the birth weight, as well as the abdominal and biparietal diameters, from the "secher" data.



if (!exists("secher")) {

  data("secher")

}


secher$log_bwt <- log(secher$bwt)

secher$log_ad <- log(secher$ad)

secher$log_bp <- log(secher$bp)


model <- lm(log_bwt ~ log_ad + log_bp, data = secher)


summary(model)


If it hasn't previously been loaded, we use this to load the "secher" data first. Subsequently, the birth weight, abdominal diameter, and biparietal diameter are log-transformed, resulting in the creation of additional variables labeled with "log_". With these log-transformed data, we then fit a linear regression model. Information about the model, including the regression coefficients you are interested in, can be found on the summary(model) line. The coefficients show the impact of the biparietal and abdominal log-transformed diameters on the log-transformed birth weight.


The summary output's "log_ad" and "log_bp" coefficient values can be used to understand the findings. As shown by your inquiry, the total of these coefficients will be close to 3. This represents the expected change in log-transformed birth weight for each unit change in log-transformed abdominal and biparietal diameters. The units of measurement and the context of your data determine how the coefficients should be interpreted.

Thursday, October 19, 2023

Module #9 Assignment

 

Module #9 Assignment


Generating a simple table for the given data.frame assignment_data:


simple_table <- assignment_data[, c("Country", "age", "salary", "Purchased")]



Generating a contingency table using the mtcars dataset:


assignment9 <- table(mtcars$gear, mtcars$cyl, dnn = c("gears", "cylinders"))



2.1. Adding row and column totals to the assignment9 table using addmargins:


assignment9_with_totals <- addmargins(assignment9)



2.2. Calculating proportional weights of each value in the assignment9 table using prop.tables:


proportional_weights <- prop.table(assignment9)



2.3. Calculating row proportions in the assignment9 table using prop.table with margin = 1:


row_proportions <- prop.table(assignment9, margin = 1)


Code & Table:


assignment_data <- data.frame(

  Country = c("France", "Spain", "Germany", "Spain", "Germany", "France", "Spain", "France", "Germany", "France"),

  age = c(44, 27, 30, 38, 40, 35, 52, 48, 45, 37),

  salary = c(6000, 5000, 7000, 4000, 8000),

  Purchased = c("No", "Yes", "No", "No", "Yes", "Yes", "No", "Yes", "No", "Yes")

)


simple_table <- assignment_data[, c("Country", "age", "salary", "Purchased")]



library(datasets)

data(mtcars)

assignment9 <- table(mtcars$gear, mtcars$cyl, dnn = c("gears", "cylinders"))



assignment9_with_totals <- addmargins(assignment9)



proportional_weights <- prop.table(assignment9)



row_proportions <- prop.table(assignment9, margin = 1)



print("Simple Table:")

print(simple_table)


print("Contingency Table with Totals:")

print(assignment9_with_totals)


print("Proportional Weights:")

print(proportional_weights)


print("Row Proportions:")

print(row_proportions)






Wednesday, October 11, 2023

Module #8 Assignment

 

Module 8 Assignment


Use the following to conduct an analysis of variance (ANOVA) in R to examine how the medicine affects stress levels:


data <- data.frame(

  StressLevel = factor(rep(c("High Stress", "Moderate Stress", "Low Stress"), each = 6)),

  StressRating = c(10, 9, 8, 9, 10, 8, 8, 10, 6, 7, 8, 8, 4, 6, 6, 4, 2, 2)

)


model <- aov(StressRating ~ StressLevel, data = data)


summary(model)


By grouping individuals into one of three stress levels and using the "aov" function to do an analysis of variance (ANOVA) with the stress rating as the dependent variable and the stress level as the independent variable, the aforementioned code will construct a data frame with the given data. The summary(model) output will include the following relevant information:

Df: Degrees of freedom, which represents the number of groups (levels of the independent variable) and the error degrees of freedom.

Sum Sq: The sum of squares, which measures the total variance in the data.

Mean Sq: The mean square, which is the sum of squares divided by degrees of freedom, and provides an estimate of variance.

F value: The test statistic for the ANOVA, which indicates whether there are significant differences between the groups.

Pr(>F): The p-value associated with the F value. A low p-value indicates statistical significance, suggesting that there are significant differences between groups.


If the p-value (Pr(>F)) is less than the selected significance level (e.g., 0.05), there are significant variations in how each group responds to stress. If this were the case, you would reject the null hypothesis that the drug's impact on stress reactions is the same regardless of the level of stress. How much the group means diverge from one another is indicated by the F value. More substantial differences are indicated by a higher F value. You can compute the mean square values using the degrees of freedom for the groups (Df) and the error term.

Saturday, October 7, 2023

Module #7 Assignment

Module 7 Assignment


1.1) Defining the relationship model between the predictor and the response variable:


The relationship model between the predictor (X) and the response variable (Y) is defined as:


Y = a + bX + e


Y = dependent variable (what is being predicted or explained).

X = independent variable (what is predicting or explaining the value of Y).

a = constant, representing the value of Y when X = 0.

b = coefficient of X, indicating the slope of the regression line, i.e., how much Y changes for each one-unit change in X.

e = the error term, which represents the error in predicting the value of Y given the value of X.


1.2) Calculating the coefficients:


To calculate the coefficients (a and b) for the given data:


x <- c(16, 17, 13, 18, 12, 14, 19, 11, 11, 10)

y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)


model <- lm(y ~ x)


a <- coef(model)[1] # Intercept (a)

b <- coef(model)[2] # Slope (b)


cat("Intercept (a):", a, "\n")

cat("Slope (b):", b, "\n")

This code fits a linear regression model to the data and extracts the coefficients a and b. You can use these coefficients to describe the relationship between X and Y.


2.1) Defining the relationship model between the predictor and the response variable:


The relationship model for the data set "visit" can be defined as:


discharge = a + b * waiting


"discharge" = dependent variable (response).

"waiting" = independent variable (predictor).

"a" = constant (intercept).

"b" = coefficient of "waiting" (slope).


2.2) Extracting the parameters of the estimated regression equation with the coefficients function:

To extract the parameters of the estimated regression equation in R:


visit <- data.frame(

  discharge = c(3.600, 1.800, 3.333, 2.283, 4.533, 2.883),

  waiting = c(79, 54, 74, 62, 85, 55)

)


model <- lm(discharge ~ waiting, data = visit)

coefficients(model)


This will give you the values of the intercept (a) and slope (b) for the estimated regression equation.


2.3) Determining the fit of the eruption duration using the estimated regression equation:

To predict the discharge duration when waiting time is 80 minutes using the estimated regression equation:


predicted_discharge <- predict(model, newdata = data.frame(waiting = 80))

cat("Predicted discharge duration for waiting time of 80 minutes:", predicted_discharge, "\n")


This will provide you with the predicted discharge duration based on the linear regression model.


3.1) Examining the relationship Multi Regression Model as stated above and its Coefficients using 4 different variables from mtcars (mpg, disp, hp, and wt):


input <- mtcars[, c("mpg", "disp", "hp", "wt")]

model <- lm(mpg ~ disp + hp + wt, data = input)

coefficients(model)


You can determine the connections between the predictors (disp, hp, and wt) and the response (mpg) by looking at the coefficients. In more detail, the coefficients show how the response variable (mpg) changes when each predictor is changed by one unit while the other predictors remain unchanged.


Plotting metabolic rate versus body weight and fit a linear regression model:


library(ISwR)

plot(metabolic.rate ~ body.weight, data = rmr)

model <- lm(metabolic.rate ~ body.weight, data = rmr)


predicted_metabolic_rate <- predict(model, newdata = data.frame(body.weight = 70))

cat("Predicted metabolic rate for a body weight of 70 kg:", predicted_metabolic_rate, "\n")


The data will be fitted to a linear regression model using this code, and you can use the resulting model to predict the metabolic rate for a 70 kg body weight.

“Ethical Concerns on the Deployment of Self-driving Cars”: A Policy and Ethical Case Study Analysis

Alec Gremer University of South Florida LIS4414.001U23.50440 Information Policy and Ethics Dr. John N. Gathegi, June 12th, 2023 “The Ethical...