?read.table #copy and paste data from another source: hotdogs=read.table("clipboard",header=T) head(hotdogs) #change directory to data file first: hotdogs2=read.table("hotdogs.txt",header=T) head(hotdogs2) #extract the column variables: type=hotdogs$Type calories=hotdogs$Calories sodium=hotdogs$Sodium #numerical summary measures: summary(type) summary(calories) summary(sodium) #statistical summary of calories by types: summary(calories[type=="Beef"]) summary(calories[type=="Poultry"]) summary(calories[type=="Meat"]) #statistical summary of sodium by types: summary(sodium[type=="Beef"]) summary(sodium[type=="Poultry"]) summary(sodium[type=="Meat"]) #Plot histograms of caolries by types: par(mfrow=c(2,2),cex=0.5) hist(calories[type=="Beef"],freq=F) lines(density(calories[type=="Beef"])) hist(calories[type=="Meat"],freq=F) lines(density(calories[type=="Meat"])) hist(calories[type=="Poultry"],freq=F) lines(density(calories[type=="Poultry"])) plot(calories ~ type) #know more about the lm command: ?lm #Fit a linear model for calories: fit.calories=lm(calories ~ type) summary(fit.calories) #Plot histograms of sodium by types: par(mfrow=c(2,2),cex=0.6) hist(sodium[type=="Beef"],freq=F) lines(density(sodium[type=="Beef"])) hist(sodium[type=="Meat"],freq=F) lines(density(sodium[type=="Meat"])) hist(sodium[type=="Poultry"],freq=F) lines(density(sodium[type=="Poultry"])) plot(sodium ~ type) #Fit a linear model sodium: fit.sodium=lm(sodium ~ type) summary(fit.sodium) #How is calories related to sodium? par(mfrow=c(2,2)) plot(calories ~ sodium) abline(lsfit(sodium,calories)) plot(calories[type=="Beef"] ~ sodium[type=="Beef"]) abline(lsfit(sodium[type=="Beef"],calories[type=="Beef"])) plot(calories[type=="Meat"] ~ sodium[type=="Meat"]) abline(lsfit(sodium[type=="Meat"],calories[type=="Meat"])) plot(calories[type=="Poultry"] ~ sodium[type=="Poultry"]) abline(lsfit(sodium[type=="Poultry"],calories[type=="Poultry"])) fit=lm(calories ~ sodium + type) summary(fit