Thursday, November 9, 2023

LIS4273 - Module 12 Assignment

 For this assignment, I will be answering the following questions:

The table below represents charges for a student credit card.

a. Construct a time series plot using R

b. Employ Exponential Smoothing Model as outlined in Avril Voghlan's notes and report the statistical outcome.

For these two parts, please see the following R code and its associated output:

c. Provide a discussion on time series and Exponential Smoothing Model result you led to.

Through the output, we can first see with the Time Series plot, it clearly shows the fluctuations in charges throughout the months and years. Furthermore, it does seem to peak in charges around mid-year, which may refer to high spending during the winter holiday months.

When we review the Exponential Smoothing model and its impact on the dataset, the first thing to take away is both parameters (beta and gamma) are set to FALSE which means that without trend and seasonality, it will emphasize the short-term variations of the data.

Looking at the high alpha value (0.8232442), this indicates that the forecasts are primarily influenced by recent observations. As for the coefficient ‘a’ value (62.44453), we can see that the model expects future values to revolve around this particular value. Now, when this new model is plotted, it more closely aligns with one another as opposed to the first plot which did not appear to be as closely aligned. Moving on the calculation of the sum-of-squared-errors (SSE), the value outputted was 835.38 which may infer that a model containing both trend and seasonality may better capture the underlying patterns in the data. Thus, it is key that you a strike a good balance between model refinement and goals of the analysis to increase model accuracy and reliability.

~ Katie


Tuesday, October 31, 2023

LIS4273 - Module 11 Assignment

 For this assignment, I will be answering the following questions:

10.1

From out textbook: pp. 188 Question 10.1

Set up an additive model for the ashina data, as part of the ISwR package

This data contains additive effects on subjects, period, and treatment. Compare the results with those obtained from t tests.

R code:

Result Interpretation:

Analyzing the results of the additive model, we first see that with the variable treat, its coefficient is -42.87 suggesting that the treatment group experienced a 42.87 unit decrease in vas as compared to the control group. As for period, its coefficient value is 80.50 which means that period 2 experienced an 80.50 unit increase in vas compared to period 1. Moving on to subjects, the first thing that sticks out are the significance levels and it is learned that for the intercept, treat, subject3, subject5, subject7, subject8, and subject10 are all significant (**) at 0.1 significance level where we can infer that their effects are not likely due to random chance. As for residuals, the range appears to go from -48.94 to positive 48.94. Ideally, these values should be randomly distributed around zero but a pattern such as this indicates the model is not capturing some aspect of data. As for the r-squared value, it is 0.7566 or 75.66% and while a good fit is a value close to 1, it means the 75.66% of the variability in vas scores is accounted for by the model. Lastly, with the F-statistic 2.914 and its associated p-value 0.02229, we can see that the model is indeed statistically significant.

Moving on to t test for treatment, we can see that there is a significant difference between the treatment and the control group via the p-value (0.02099). As for the t test for period, it is only marginally significant with the p-value (0.0672).

10.3 Consider the following definitions


Note:

The rnorm() is a built-in R function that generates a vector of normally distributed random numbers. The rnorm() method takes a sample size as input and generates that many numbers.

Your assignment:

Generate the model matrices for models z ~ a*b, z ~ a:b, etc. In your blog posting discuss the implications. Carry out the model fits and notice which models contain singularities.

Hint

We are looking for...

model.matrix(~ a:b); lm(z ~a:b)

R code:

Result Interpretation:

Through executing the code, the only model that contained singularities is model2 which held the expression a:b. The rest of the models yielded false when asked if they held singularities. What are the implications of this? Well, first off, it means the model2 has perfect collinearity. In other words, one predictor can be exactly predicted from the other which may lead to numerical instability in estimating the coefficients. Additionally, singularity means that there is an infinite number of solutions to model and R cannot uniquely determine the coefficients of a and b. Further, because a and b cannot be separated due to collinearity, the coefficient estimates will be unreliable.

As for the other models which yielded false when prompted for collinearity, we know that the coefficient estimates are reliable. Seeing that only one model had perfect collinearity and the rest did not, it might be best to focus on the non-collinear models for future analysis.

~ Katie

Monday, October 23, 2023

LIS4273 - Module 10 Assignment

 For this assignment, I will be answering the following questions:

Question 1:

From our textbook, Introductory Statistics with R pp. 159 Exercises, 9.1 and 9.2

9.1 I revised this question , so please follow my description only. Conduct ANOVA (analysis of variance) and Regression coefficients to the data from cystfibr (> data("cystfibr")) database. You can choose any variable you like. in your report, you need to state the result of Coefficients (intercept) to any variables you like both under ANOVA and multivariate analysis. I am specifically looking at your interpretation of R results.

Extra clue:

The model code:

In R:

Interpretation of the results:

Reviewing the output of the model, we can see that in terms of significance, the intercept is highly significant while the other variables: weight, bmp, and fev1 are all significant. Reading further into the results, we can take away the following ideas:

With age, we can see the pemax (maximum expiratory pressure) will decrease by approximately -3.4181 units for every one unit increase in age. However, the R results do not deem this variable as significant so we must move on.

As for weight, pemax (maximum expiratory pressure) will increase by 2.6882 units for every one unit increase in weight. R views this variable as statistically significant so this variable may be quite helpful in future models.

For bmp, which is body mass, the pemax variable will decrease by -2.0657 units for every one unit increase in bmp. This variable is also statistically significant and will definitely be helpful in future analyses.

Lastly, with fev1, which refers to forced expiratory volume, pemax will increase by 1.0882 units for every one unit increase in fev1.

Taking into account the p-values for each of the variables it seems that only fev1 gets rather close to 0.05 significance level at around 0.04695. The rest of the variables and their corresponding p-values are lower than fev1 with the exception of age, but that value is not at all significant to the analysis.

Moving on to the ANOVA table and its output, there are some surprising results. Looking first to the sum of squares which refers to concept of that a higher sum of squares equates to greater variability in the data. The variable age proves to have the highest value at 10098.5. The rest of the variables do not have as high of a sum of squares value. As we review each of the p-values and their corresponding significance levels we immediately see that age is highly significant at the three-star (***) range. Additionally, bmp and fev1 are considered as only marginally significant while there are no stars at all given to weight. Hence, age is a highly significant predictor and not far behind it are bmp and fev1 which alludes to the idea that they contribute to the variability in pemax variable.

Question 2:

9.2 The secher data (> data("secher")) are best analyzed after log-transforming birth weight as well as the abdominal and biparietal diameters. Fit a prediction weight as well as abdominal and biparietal diameters. For a prediction equation for birth weight.

How much is gained by using both diameters in a prediction equation?

The sum of the two regression coefficients is almost identical and equal to 3.

Can this be given a nice interpretation to our analysis?

Please provide a step by step on your analysis and code you use to find out the result.

Extra clue:

In R:

Interpretation of the results:

Analyzing the output of this model, we can see that when using both parameters bwt (birth weight) and ad (abdominal) in the prediction equation, it can be said that the log of ad indeed contributes to the prediction of log(bwt). As we look at the output, most notably the coefficients section, for each one unit increase of log(ad), the estimated change in log(bwt) is 2.2365. Now, given that the sum of the two regression coefficients is almost identical and equal to 3, this suggests that the model is inferring that the total effect on log(bwt) is additive when log(ad) as well as the intercept are considered. Just for reference, additive simply means that the effect of one predictor variable on the response variable is independent of the values of the other predictor variables. Moving on to the R-squared value, it is 0.7959 which means that 79.59% of the variability in log(bwt) can be explained by the model. As for the F-statistic, seeing its high value and the low p-value, one can infer that the model is indeed statistically significant.

~ Katie

Friday, October 20, 2023

LIS 4273 - Module 9 Assignment

For this assignment, I will be answering the following questions:

Question 1:

Your data frame is

 


Generate a simple table in R that consists of four rows: Country, age, salary, Purchased


Question 2

Generate a contingency table also known as rx C table using the mtcars dataset

Note: the following sections of this question can be found within the gist:

~ Katie

Tuesday, October 10, 2023

LIS4273 - Module 8 Assignment

 For this post, I will answering the following questions.

Question 1:

A researcher is interested in the effects of drug against stress reaction. She gives a reaction time test to three different groups of subjects: one group that is under a great deal of stress, one group under a moderate amount of stress, and a third group that is under almost no stress. The subjects of the study were instructed to take the drug test during their next stress episode and to report their stress on a scale of 1 to 10 (10 being most pain).



Report on the drug and stress level by using R. Provide a full summary report on the result of ANOVA testing and what does it mean? More specifically, report using the following R functions:


After running a summary report after using ANOVA, we first see that the degrees of freedom results in 2 meaning that there are two degrees of freedom associated with the variability in stress levels because of the stress group. As for Sum Sq or Sum of Squares, the value 82.11 represents the sum of squares associated with the stressGroup variable. Given the larger value, it can be interpreted that there is a significant amount of variability in stressLevel that is being explained by the stressGroup. Moving on to Mean Sq, 41.06 is the value associated with the stressGroup variable. Seeing how the value is larger than the residuals value, it can be taken away that the stress group indeed explains more variability in stressLevel than expected by random chance. For the F value, the summary produced the value of 21.36 which seems to indicate that it is a large value coupled with a rather small p-value (4.08e-05). Thus, the stress group variable has a statistically significant effect on stress levels.

Question 2:

From our Textbook: Introductory Statistics with R, Chapter 6, Exercises 6.1, pp. 127

The zelazo data (taken from textbook's R package IWsR) are in the form of a list of vectors, one for each of the four groups. Convert the data to a form suitable for the user of lm and calculate the relevant test. Consider t tests comparing selected subgroups or obtained by combining groups.

2.1 Consider ANOVA test (one-way or two-way) to this dataset (zelazo)

Recommendations



After reflecting upon the zelazo dataset, it can be seen that the one-way ANOVA test is more appropriate given that data set contains only one independent variable (type of training) with four levels like “active”, “passive”, “none”, and “ctr.w8” which means control. Seeing that one-way ANOVA tests work best when given a categorical independent variable (factor) with more than two levels (groups), it is clear that this test is more fitting to the problem than two-way ANOVA. We have four groups and one factor.

~ Katie

Saturday, October 7, 2023

LIS4273 - Module 7 Assignment

 For this assignment, I will be answering the following questions:

Question 1:

In this assignment's segment, we will use the following regression equation:

Y = a + bX + e

Where:

Y is the value of the Dependent variable (Y), what is being predicted or explained

a or Alpha, a constant; equal the value of Y when the value of X = 0

b or Beta, the coefficient of X; the slope of the regression line; how much Y changes for each one-unit change in X.

X is the value of the Independent variable (X), what is predicting or explaining the value of Y.

e is the error term; the error in predicting the value of Y, given the value of X (it is not displayed in most regression equations).

A reminder about the lm() function:

lm([target variable] ~ [predictor variables], data = [data source])

1.1

The data in this assignment:

1.1 Define the relationship model between the predictor and the response variable

To determine the predictor and response variables, we can simply plug the two datasets into R and use the lm function to define the relationship model. Analyzing the output via the summary function, we can conclude that there is a positive linear relationship between x and y. If we notice the asterisk (*) on the x row, the predictor value x is statistically significant at the 0.5 significance level. All in all, the predictor variable (x) makes a statistically significant impact on the response variable (y).

1.2 Calculate the coefficients

To calculate the coefficients, we can use the coefficients function to pull those coefficients directly

Question 2:

The following question is posted by Chi Lau...

Apply the simple linear regression model(see the above formula) for the data set called "visit" (see below) and estimate the discharge duration if the waiting time since the last eruption has been 80 minutes.

Employ the following formula: (discharge ~ waiting and data = visit)

Please see the given coding solution in R:

2.1 Define the relationship model between the predictor and the response variable

Interestingly, when we look at waiting, our predictor variable, it’s rather meaningful as indicated by the low p-value (<2e-16). This allows us to see that waiting is indeed a meaningful predictor of the response variable “discharge”.

2.2 Extract the parameters of the estimated regression equation with the coefficients function.

2.3 Determine the fit of the eruption duration using the estimated regression equation.

To understand the fit of the eruption duration, we can look at the summary of the model. Just looking at the Residuals section of the output, the values tell us that there are extreme outliers presents which means there might be issues within the model. When we examine the multiple R2, we see that it is 0.8115 or 81.15%. This value indicates to us that the variability in discharge is explained by the model and the higher the value, the better the fit. However, this high value is penalized by the adjusted R-squared which is 0.8108. Nonetheless, the given estimated regression equation provides a good fit of the eruption duration.

Question 3: Multiple Regression

We will use a very famous datasets in R called mtcars. This dataset was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973--74 models).

This data frame contains 32 observations on 11 (numeric) variables.

To call mtcars data in R

R comes with several built-in data sets, which are generally used as demo data for playing with R functions. One of those datasets build in R is mtcars.

In the question, we will use four of the variables found in mtcars by using the following function:

R will display

3.1 Examine the relationship Multi Regression Model as stated above and its Coefficients using 4 different variables from mtcars (mpg, disp, hp and wt). Report on the result and explanation what does the multi regression model and coefficients tells about the data?

Looking at the following output via summary:

As we analyze the results, we can immediately note from the coefficients section of the data that disp, hp, and wt are all negative values. What does this mean? Well, starting with disp, we can see that there is no linear relationship between mpg and is close to zero (-0.000937). As for hp, that value is (-0.031157) and what we can derive from that is as hp increases, mpg decreases which is a fairly reasonable observation. Lastly, with wt (-3.800891) it shows us that as wt increases, mpg decreases.

Question 4:

From our textbook pp. 110 Exercises # 5.1

With the rmr data set, plot metabolic rate versus body weight. Fit a linear regression to the relation. According to the fitted model, what is the predicted metabolic rate for a body weight of 70 kg?

The data set rmr is in R, make sure to install the book R package: ISwR. After installing the ISwR package, here is a simple illustration to the set of the problem.

Looking at the data points on the scatter plot, it appears to suggest a strong positive linear relationship. For a body weight of 70 kg, we can see that it corresponds to a predicted metabolic rate of around 1300.



To see the exact value, we execute the following code to get the exact metabolic rate (please see lines 19 through 26 and its corresponding output).

~ Katie

Saturday, September 30, 2023

LIS4273 - Module 6 Assignment

 For this assignment, I will be answering the following questions:

A.

Consider a population consisting of the following values, which represents the number of ice cream purchases during the academic year for each of the five housemates. 8, 14, 16, 10, 11

For the four parts of the question, I will embed each of the sub questions within the following R code.

B.

Suppose that the sample size n = 100 and the population proportion p = 0.95.

1. Does the sample proportion p have approximately a normal distribution? Explain.

When it comes to determining if sample proportion p has an approximate normal distribution, it is important that we refer to the Central Limit Theorem which states that the larger the given sample size, the greater the chance that the sample proportion will have a normal distribution. Additionally, it is often said that if the population proportion is very close to 0 or 1, there is a higher likelihood that there must be larger sample size for there to be a normal distribution.

Given that the sample size n is 100 and the population proportion p is 0.95, this sort of scenario should be suitable enough to render a normal distribution. However, to assure that there is straight normality, a larger sample size would be needed to achieve an approximate normal distribution.

Let's break this idea down mathematically:

Using the above values, let’s solve for np and nq and determine whether it is greater than 10 for the distribution to be normal.

np = 100 * 0.95 = 95

nq = 100 * (1 – 0.95) = 5

Only np is greater than 10 so we can conclude that this may not exactly be a normal distribution. While these circumstances may not be completely unreasonable, if precision is prioritized then a larger sample size is needed.

2. What is the smallest value of n for which the sampling distribution of p is approximately normal?

To answer this question, I must preface that there is no one-size-fits-all answer when it comes achieving the smallest possible value for n for which the sampling distribution is normal. Typically, the smallest value of n is dependent on the population proportion and the desired level of approximation. Many sources have suggested n to be greater than 10 but determining the best value varies depending on the population distribution and how close p is to 0 or 1.

The sample mean from a group of observations is an estimate of the population mean μ . Given a sample of size n, consider n independent random variables X1, X2, ..., Xn, each corresponding to one randomly selected observation. Each of these variables has the distribution of the population, with mean μ and standard deviation σ.

A. Population mean = (8 + 14 + 16 + 10 + 11) / 5 (5 represents the number of values in the set)

B. Sample of size n = 5

C. Mean of sample distribution = 11.8

We can put together some samples using some R code:

 And Standard Error Qm / Q

           Square root of n 4.4 / square root of 5

D. I am looking for table with the following variables X, x=u, and (x-u)^2

Here's a little hint

The sample size n =100 and the population proportion p = 0.95

Does the sample proportion p have approximately a normal distribution? The distribution is expected to be normal if both np and nq are greater ....... (Your Turn)

Since p = .95, q = .05.

p * n = .95 * 100 = 95

q * n = .05 * 100 = 5

It is often said that there must be some kind of benchmark or value to determine normality. To have a reasonably decent normal distribution, we can refer to classic Central Limit Theorem guidelines that state that np and nq should be greater than or equal to 10. Seeing that nq is not greater than 10, this tells us that the sample proportion does not have an approximately normal distribution. What can be taken away from this is that more information is needed based on the precision level and context to determine that this is a reasonable enough normal distribution.

C.

From our textbook, Chapter 2 Probability Exercises # 2.4 Simulated coin tossing is probability better done using function called rbinom than using function called sample. Explain.

Comparing the functions rbinom and sample, we can immediately see that in the parameters taken in by rbinom, it is better equipped to handle something like a simulated coin toss as contrasted with sample. From the textbook and this week’s lecture slides, the binomial distribution maintains constant probability with each trial and these trials are always independent. Furthermore, rbinom is more suited to handle complex statistical scenarios where there are multiple trials or varying levels of probabilities for each trial. From a functional standpoint, rbinom is much more powerful as sample is a bit more general in its function.

~ Katie

LIS 4370 R Programming - sentimentTextAnalyzer2 Final Project

For this class's major final project, I set out to make the process of analyzing textual files and URL links for sentiment insights much...