R - 自变量和多项式项之间的相互作用

时间:2017-11-18 23:40:50

标签: r regression interaction

我感兴趣的是将一个自变量( sex )与持续时间的数字自变量(工作时间)进行交互。

我不确定正确的方法是什么。

y = sex * working_hours + I(working_hours^2) 

或者我是否需要互动两个术语

y = sex * working_hours + sex * I(working_hours^2)  

我有兴趣在这里做一个混合模型

lmer(y ~ sex * working_hours + I(working_hours^2) + (1 | id), data = df)

lmer(y ~ sex * working_hours + sex * I(working_hours^2) + (1 | id), data = df)

感谢。

1 个答案:

答案 0 :(得分:1)

使用正确的公式取决于您尝试实现的目标。

override func viewDidLoad() { super.viewDidLoad() databaseRef = Database.database().reference().child("Rentals") databaseRef?.observe(.childAdded, with: { (snapshot) in if let dictonary = snapshot.value as? [String: AnyObject] { print(snapshot) switch snapshot.key { case "bond" : _ = dictonary.map{self.rentalsObject.bond.append(($0.value as? String)!)} // print(self.arrayOfBond) case "dateAval" : _ = dictonary.map{self.rentalsObject.dateAval.append(($0.value as? String)!)} case "description" : _ = dictonary.map{self.rentalsObject.descripton.append(($0.value as? String)!)} case "location" : _ = dictonary.map{self.rentalsObject.location.append(($0.value as? String)!)} case "pets" : _ = dictonary.map{self.rentalsObject.pets.append(($0.value as? String)!)} case "price" : _ = dictonary.map{self.rentalsObject.price.append(($0.value as? String)!)} case "rentalType" : _ = dictonary.map{self.rentalsObject.rentalType.append(($0.value as? String)!)} case "title" : _ = dictonary.map{self.rentalsObject.title.append(($0.value as? String)!)} print(self.rentalsObject.title) // _ = dictonary.map{self.arrayOfTitles.append($0.value as? String)} // print(self.arrayOfTitles) default: break } } whateverCollectionIsYourInCacheDataSource = whateverCollectionIsYourInCacheDataSource.sorted(by: { ($0, $1) -> Bool in return $0 < $1 //If you want to sort them by their date value. Make sure the dates are properly interpreted from whatever format they're saved in remotely into the Date type. }) DispatchQueue.main.async { //No need to call .reloadData and these methods in viewDidLoad, unless you REALLY have to (ie: your data is modified on purpose after being loaded initially). Calling these methods loads up the data anyways, so just call these whenever your data is downloaded, converted and sorted properly. tableView.dataSource = self tableView.dataSource = self } }) } 将允许y = sex * working_hours + I(working_hours^2)和工作时间之间关系的线性部分变化y,而两性关系的二次部分将相同。换句话说,如果你为每个性别画出合适的线条,那就是“弯曲”。这种关系是一样的,但是“倾斜”是关系。曲线会有所不同。

sex将允许关系的线性和二次部分按性别变化。如果你为每个性别绘制了拟合的线条,那么“倾斜”和“倾斜”都是如此。和弯曲&#39;两性之间的关系会有所不同。

以我的思维方式,后者更有意义 - 如果我们允许线性部分变化,为什么我们会假设两性关系的二次部分应该是相同的?