###R code for Lecture 19 - relationship between T-tests and Regression ### import the data datum=read.csv(file.choose()) head(datum) ### Note that x-variable can be dummy coded, but doesn't have to be. ### If not dummy coded, R will dummy code for you when it analyzes data ### Also note that with newer versions of R (Version 4.0 or greater), ### R doesn't automatically treat words at categorical x variables ### To get R to treat words as a categorical variable, you must convert ### the variable to a 'factor' datum$X=as.factor(datum$X) # Replace 'X' with the variable name ### Plot the data plot(Y~X,data=datum) ### Replace 'Y' with y-variable, and 'X' with X variable ### Run a regression with categorical x (technically a t-test) results=lm(Y~X,data=datum) ### Replace 'Y' with y-variable, and 'X' with X variable summary(results) ### Note that if X is a text variable you might have to figure out what your 'reference' is ### Get confidence intervals confint(results) ### Predictions when x is categorical are the same as when x is continuous, ### but x must be set to a categorical group NewX=data.frame(X="group1") #where 'X' is the name of your x variable and "group1" is the group you want to make ### predictions for. ### Note that "group1" must be in quotes predictions=predict(results,NewX,interval="prediction") ### If you want to make predictions for all groups: NewX=data.frame(X=c("group1","group2","group3"))